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

In view

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

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

In model

// 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.

tereško
  • 58,060
  • 25
  • 98
  • 150
saji
  • 201
  • 1
  • 5
  • 19
  • possible duplicate of [What is wrong with the statement,in Yii?](http://stackoverflow.com/questions/23009979/what-is-wrong-with-the-statement-in-yii) – topher Apr 11 '14 at 10:44

2 Answers2

0

You define $model 3 times:

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

Then

$model = new Viewjob('search');

And

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

Choose one that you need, better last. And add to controller

$model->user_id = $user_id

It will works.

Alex
  • 8,055
  • 7
  • 39
  • 61
  • ok...alex..now I am using /* $model=ViewJob::model()->findByAttributes(array('user_id'=>Yii::app()->user->id)); */ and what do in views. – saji Apr 11 '14 at 09:58
  • alexk984..but when i use findAll its showing error "Property "CDbCriteria.user_id" is not defined., while using findByAttributes its displaying the entire result. – saji Apr 11 '14 at 10:30
  • Delete $model= ViewJob::model()->findAll.... and $model = new Viewjob('search'); – Alex Apr 11 '14 at 10:55
0

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

And in View, simply:

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

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('*'),
            ),
        );
    }
Vishal Khode
  • 831
  • 2
  • 9
  • 15
  • now getting errror "Call to a member function getData() on a non-object in C:\wamp\www\yii\framework\zii\widgets\CBaseListView.php on line 11" – saji Apr 12 '14 at 04:41
  • Actually that should work, but try changing: ` $model = ViewJob::model()->findAll($criteria); ` with ` $model = New CActiveDataProvider('ViewJob', array('criteria' => $criteria)); ` – Vishal Khode Apr 12 '14 at 12:39