Cgridview Didn't Work Dropdown Filter

$this->client_id didn’t pass the validation, when I change $criteria->compare(‘client_id’,$this->client_id); to $criteria->compare(‘client_id’,$_REQUEST[‘Tasks’][‘client_id’]); in search() all work fine, but how to fix my code?

My database relations in attach

My model:




public function rules()

    {

        // NOTE: you should only define rules for those attributes that

        // will receive user inputs.

        return array(

            array('client_id, task', 'required'),

            array('client_id, complete', 'numerical', 'integerOnly'=>true),

            array('task', 'length', 'max'=>5000),

            array('start_date','default',

                'value'=>new CDbExpression('NOW()'),

                'setOnEmpty'=>false,'on'=>'insert'),

            array('task_id, client_id, task, start_date, end_date, complete', 'safe', 'on'=>'search'),

        );

 public function relations()

    {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

            'client' => array(self::BELONGS_TO, 'Client', 'client_id'),

        );

    }


    public function search()

    {

        // Warning: Please modify the following code to remove attributes that

        // should not be searched.


        $criteria=new CDbCriteria;

        $criteria->with = array('client');

        $criteria->compare('task_id',$this->task_id);

        $criteria->compare('client_id',$this->client_id);

        $criteria->compare('name',$this->client->name);

        $criteria->compare('task',$this->task,true);

        $criteria->compare('start_date',$this->start_date,true);

        $criteria->compare('end_date',$this->end_date,true);

        $criteria->compare('complete',$this->complete);


        return new CActiveDataProvider($this, array(

            'criteria'=>$criteria,

        ));

    }



and my view:




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

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

    'filter' => $model,

    'columns'=>array(

        array(

            'name' => 'client_id',

            'filter' => CHtml::listData(Client::model()->findAll(), 'client_id', 'name'),

            'value'=>'$data->client->name'

        ),

        'task'

     )

));



what validation error you get?

Try this - taken from here




$criteria->with = array('task');

$criteria->compare('task.id', $this->task_id);



Matt

I have not any validation error, when I changed value in dropdown filter - the data in table didn’t changed. I tried trick from wiki, but also no result.

Post your Controller and View code, please.

Controller




<?php


class TasksController extends Controller

{

	public function actionIndex()

	{

        $model=new Tasks;

		$this->render('index',array(

            'model'=>$model

        ));

	}


    public function actionCreate()

    {

        $model=new Tasks;

        if(isset($_POST['Tasks']))

        {

            $model->attributes=$_POST['Tasks'];

            if($model->save())

                $this->redirect(array('view','id'=>$model->task_id));

        }


        $this->render('index',array(

            'model'=>$model,

        ));

    }


    public function actionView($id){

        $this->render('view',array(

            'model'=>$this->loadModel($id),

        ));

    }


    public function actionFinish(){

        if(isset($_POST['Tasks']['task_id'])) {

            $id=$_POST['Tasks']['task_id'];

            $model=$this->loadModel($id);

            $model->complete=1;

            $model->end_date = new CDbExpression('NOW()');

            if($model->save())

                $this->redirect(array('view','id'=>$model->task_id));


        }


    }


    public function loadModel($id)

    {

        $model=Tasks::model()->findByPk($id);

        if($model===null)

            throw new CHttpException(404,'The requested page does not exist.');

        return $model;

    }


	// Uncomment the following methods and override them if needed

	/*

	public function filters()

	{

		// return the filter configuration for this controller, e.g.:

		return array(

			'inlineFilterName',

			array(

				'class'=>'path.to.FilterClass',

				'propertyName'=>'propertyValue',

			),

		);

	}


	public function actions()

	{

		// return external action classes, e.g.:

		return array(

			'action1'=>'path.to.ActionClass',

			'action2'=>array(

				'class'=>'path.to.AnotherActionClass',

				'propertyName'=>'propertyValue',

			),

		);

	}

	*/

}



View




<?php

/* @var $this TasksController */


$this->breadcrumbs=array(

	'Tasks',

);

$this->beginWidget('CActiveForm', array(

    'id'=>'user-form',

    'enableAjaxValidation'=>false,

    'htmlOptions'=>array('enctype'=>'multipart/form-data'),

));

echo CHtml::errorSummary($model);

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

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

    'filter' => $model,

    'columns'=>array(

        array(

            'name' => 'client_search',

            'filter' => CHtml::listData(Client::model()->findAll(), 'client_id', 'name'),

            'value'=>function($data) {

                return $data->client->name;

            }

        ),

        'task'

     )

));




        echo CHtml::submitButton('Login');




$this->endWidget('CActiveForm');



Model




<?php


/**

 * This is the model class for table "tasks".

 *

 * The followings are the available columns in table 'tasks':

 * @property integer $task_id

 * @property integer $client_id

 * @property string $task

 * @property string $start_date

 * @property string $end_date

 * @property integer $complete

 *

 * The followings are the available model relations:

 * @property Client $client

 */

class Tasks extends CActiveRecord

{

    public $client_search;




	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return Tasks the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'tasks';

	}


    public function finish(){

        echo    'www';

    }


        /**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('client_id, task', 'required'),

			array('client_id, complete', 'numerical', 'integerOnly'=>true),

			array('task', 'length', 'max'=>5000),

            array('start_date','default',

                'value'=>new CDbExpression('NOW()'),

                'setOnEmpty'=>false,'on'=>'insert'),

			array('task_id, client_search, task, start_date, end_date, complete,client_id', 'safe', 'on'=>'search'),

		);


	}


    public function getClient_Search() {

        return $this->client_search;

    }

	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'client' => array(self::BELONGS_TO, 'Client', 'client_id'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'task_id' => 'Task',

			'client_id' => 'Client',

			'task' => 'Task',

			'start_date' => 'Start Date',

			'end_date' => 'End Date',

			'complete' => 'Complete',

            'client_search'  => 'Клиент'

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{


		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;

        $criteria->with = array('client');

		$criteria->compare('t.task_id',$this->task_id);

		#$criteria->compare('client_id',$this->client_id);

        $criteria->compare('t.client_id', $this->client_search);

        $criteria->compare('t.name',$this->client->name);

		$criteria->compare('t.task',$this->task,true);

		$criteria->compare('t.start_date',$this->start_date,true);

		$criteria->compare('t.end_date',$this->end_date,true);

		$criteria->compare('t.complete',$this->complete);

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

            'sort'=>array(

                'attributes'=>array(

                    'client_search'=>array(

                        'asc'=>'client.client_id',

                        'desc'=>'client.client_id DESC',

                    ),

                    '*',

                ),

            ),

		));

	}

}



Which controller action is rendering this view? If you look at a typical Admin action, it collects $_GET variables and populates the model, thereby creating a filter. My point is, I don’t see any action that collects data and populates a model - except for actionCreate; but it’s doing a redirect so I assume it’s not that one.





public function actionAdmin()

{

    $model = new Inventory('search');


    $model->unsetAttributes(); // Clear default values


    if (isset($_GET['Inventory']))

        $model->attributes = $_GET['Inventory']; // This is where the filtering is being done


    $this->render('admin', array(

        'model' => $model

    ));

}



Matt

I understand my mistake, I change my controller and all work fine! Thx all.




public function actionIndex()

        {

        $model=new Tasks;

        if(isset($_REQUEST['Tasks']))

            $model->attributes=$_GET['Tasks'];

                $this->render('index',array(

            'model'=>$model

        ));

}