What is wrong with the statement,in Yii?

大兔子大兔子 提交于 2019-12-11 06:58:52

问题


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

         ),?>

回答1:


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().



来源:https://stackoverflow.com/questions/23009979/what-is-wrong-with-the-statement-in-yii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!