本文介绍了从 yii 中的数据库中检索特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am working on a job site,And want to show only the jobs posted by a particular user in cgridview.My actuall aim is to authenticate the user so that only jobs posted by him/her will be visible in cgridview.I have done the following stuff,but not working.

In controller:

     public function actionViewJob() {

        $user_id = Yii::app()->session['user_id'];


        /* For User Authentication */
        if (Yii::app()->user->getId() === null)
            $this->redirect(array('site/login'));
        /* For User Authentication */
/* Have tried the following codes to filter */

      $model=  ViewJob::model()->findAll(array(
                                            'select'=>'*',"condition"=>"user_id='$user_id'",
                                            ));
     // $model=ViewJob::model()->findByAttributes(array('user_id'=>Yii::app()->user->id));
       //  $model = ViewJob::model()->findAll("user_id=$user_id");


        $model = new Viewjob('search');

        $params = array('model' => $model,
        );

        $this->render('viewjob', $params);
    }

在视图中

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' =>$model->search()

//'filter' => $model,/* 没有使用这个选项,所以注释掉它 */))

// 'filter' => $model, /* not using this option ,so commented it */))

在模型中

//我真的需要这个函数吗//公共函数搜索(){

// Do I really Need This Function // public function search() {

    $criteria = new CDbCriteria;

   $criteria->compare('user_id', $this->user_id, true);
    return new CActiveDataProvider('viewjob', array(
       'criteria'=>$criteria,

    ));
},,

我在这里做错了什么.它仍在获取表中的所有可用行.

What am I doing wrong here.It is still fetching all the available rows in table.

推荐答案

创建新的 CDbCriteria 对象并使用它添加条件并将其传递给模型.在控制器中:

Create new CDbCriteria object and add condition using it and pass it to model.In Controller:

public function actionViewJob() {
  $criteria = new CDbCriteria ();
  $criteria->condition = 'user_id=' . Yii::app()->user->id;
  $model  = ViewJob::model()->findAll($criteria);
  $params = array('model' => $model);
  $this->render('viewjob', $params);
}

在 View 中,只需:

And in View, simply:

$this->widget('zii.widgets.grid.CGridView', array(
 'dataProvider' =>$model

同样用于身份验证,在您的控制器中,您无需检查,如果用户具有用户 ID,只需添加访问规则,这将自动将用户重定向到登录页面以查看作业,并在登录后 -in,将它们返回到同一页面.所以,把它添加到我们控制器的顶部..

Also for use Authentication, in your controller you don't need to check, if user has the user id, simply add access rules, which will automatically redirect user to the login page to view the job and once they are logged-in, will return them to the same page. So, add this at the top of our controller..

class YourController extends Controller {

    public function filters() {
        return array(
            'accessControl', // perform access control
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('index', 'view'),
                'users' => array('*'),
            ),
            array('allow', // allow authenticate user actions
                'actions' => array('viewjob'),
                'users' => array('@'),
            ),
           array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

这篇关于从 yii 中的数据库中检索特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:30