Восстановление определенной строки из базы данных в 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, / * не используя эту опцию, поэтому прокомментировал ее * /))

В модели

// Мне действительно нужна эта функция // public function search () {

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

Что я делаю неправильно здесь. Он все еще извлекает все доступные строки в таблице.

Solutions Collecting From Web of "Восстановление определенной строки из базы данных в yii"

Вы определяете $ model 3 раза:

 $model= ViewJob::model()->findAll(array( 'select'=>'*',"condition"=>"user_id='$user_id'", )); 

затем

 $model = new Viewjob('search'); 

А также

 'dataProvider' =>$model->search() 

Выберите тот, который вам нужен, лучше. И добавить к контроллеру

 $model->user_id = $user_id 

Он будет работать.

Создайте новый объект CDbCriteria и добавьте условие, используя его, и передайте его модели. В контроллере:

 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); } 

И в режиме просмотра просто:

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

Также для использования Аутентификация в вашем контроллере вам не нужно проверять, имеет ли пользователь идентификатор пользователя, просто добавляет правила доступа, которые автоматически перенаправляют пользователя на страницу входа, чтобы просмотреть задание, и после того, как они вошли в систему, верните их на ту же страницу. Итак, добавьте это в верхней части нашего контроллера.

 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('*'), ), ); }