phpyiiyii-componentszii-widgets

What is wrong with the statement,in Yii?


I have a folowing code in my 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 */

         $model=ViewJob::model()->findAll(array('user_id'=>Yii::app()->user->id));
        $params = array('model' => $model,
        );

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

I am getting error /* Property "CDbCriteria.user_id" is not defined. */ but when I am using findByAttrinute its working f9,but I not getting the result,i.e its not filtering data,pls help.

view section :

// not posting full code //


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

'columns' => array(
            array(
                'name' => 'title',
                'type' => 'raw',
                'value' => 'CHtml::encode($data->title)',
                'htmlOptions' => array('style'=>'width:90px;','class'=>'zzz'),
               // 'filter'=>'false' /* for hiding filter boxes */

         ),?>

Solution

  • Wrong method. You should use CActiveRecord::findAllByAttributes() instead

    $model=ViewJob::model()->findAllByAttributes(array('user_id'=>Yii::app()->user->id));
    

    Or if you still want to use findAll you should pass the attribute in as a condition:

    $model=ViewJob::model()->findAll('user_id=:user_id',array(':user_id'=>Yii::app()->user->id));
    

    Now for the view:

    dataProvider expects an instance of CDataProvider. CActiveRecord::search() returns one such instance: CActiveDataProvider. However, $model is an array not an instance of CActiveRecord. You have two choices here:

    a) You can to edit your controller to use $model as a ViewJob instance and not an array of ViewJob instances:

    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 */
    
        $model= new ViewJob;
        $model->user_id = $user_id;
        $params = array('model' => $model,
        );
        ...
    

    b) Replace the $model->search() in the view with

        'dataProvider' => new CArrayDataProvider($model)
    

    Choice (b) is easier but it breaks conventions e.g $model is an array not an object. Also any filters/search functionality has to be added manually as opposed to using the default functionality provided by CActiveRecord::search().