Getting filter to work in CGridView

Hello, I originally had the following in my model search method:




//basically the default, just grabs the attributes

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

$criteria->....

));



and then for a CGridView on one of my pages:




$model=new Engines('search');

$model->unsetAttributes();

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

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


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

				'id'=>'engines-grid',

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

				'filter'=>$model,

				'columns'=>array(

					'engine',

				),

			));



However, I had to change my search() method to something else, and now the CGridView filter doesn’t work at all:




$criteria->alias = 'e1';

$criteria->distinct = true;

$criteria->condition = "e1.build=(SELECT MAX(e2.build) FROM Engines e2 WHERE e1.engine=e2.engine



I don’t get it, I’m still using $model->search() and $model for my filter, so wouldn’t it be getting the correct values based on my criteria? The sort works fine, it’s just the filter that doesn’t do anything. Any help is appreciated, thanks.

Hi rymonator,

Can you provide the full code of the following:

  1. Model "search()" method.

  2. Action method which display the view containing CGridView.

  3. View file it self.

Thank you,

Yohan

Hi,

I have the same problem, did you solve yours ?

model:


public function search()

	{


		$criteria=new CDbCriteria;


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

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

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

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

                $criteria->condition='idOwner=:idOwner';              //the 2 lines

                $criteria->params=array(':idOwner'=>$this->idOwner);  //added...


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,                          

                ));

	}

action:


public function actionAdmin()

	{

		$model=new Resume('search');

		$model->unsetAttributes();  // clear any default values                

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

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

                        $model->idOwner = $this->_Owner->id;

                        


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

			'model'=>$model,

		));

	}

view:




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

	'id'=>'resume-grid',

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

	'filter'=>$model,

	'columns'=>array(

                'id',

                'title',

		'datePosted',

		'active',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



With this code the list take into account the ownerId so it works fine, but the filter doesn’t work anymore!!

If I comment the 2 lines the filter works normaly… What am I missing ?

Check the documentation for compare() - http://www.yiiframew…ondition-detail

It say:

by adding the line


$criteria->condition='idOwner=:idOwner';

You are loosing the previous value of this property !

So…

use ".=" to add your condition… instead of "=" to assign your condition…

and don’t forget to add a space before your condition… as this is just a string concatenate…

Ok thank you very much for answer so quickly,

in fact i just need to add a simple compare():


public function search()

        {


                $criteria=new CDbCriteria;


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

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

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

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

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


                return new CActiveDataProvider(get_class($this), array(

                        'criteria'=>$criteria,                          

                ));

        }

Now it works perfectly fine

so stupid…