CGridView Filtering related tables


I have a problem with CGridView, the grid show the textbox filter but when i tip something and then press confirm, the grid doesn’t filter the data.

here is my code

/*the Controller*/

$dataProvider=new CActiveDataProvider('DMSenha',



							'select'=>'t.id_vida, t.id_senha, count(id_senha) as total_senhas',

							'join'=>'LEFT JOIN vida as v ON  (v.id_vida = t.id_vida)',











/*the View*/

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

				'dataProvider'=> $dataProvider,






						'filter' => CHtml::textField('vida.nome_vida'), 

           				 'value' => 'DMVida::Model()->FindByPk($data->id_vida)->nome_vida',







					$id_senha = $(\'#gridsenha\').yiiGridView.getSelection(id);






					}'	,	


/*The Relation in DMSenha*/

'vida' => array(self::BELONGS_TO, 'DMVida', 'id_vida'),

I need to filter the vida.nome_vida and vida.cod_vida columns.

I have already looked at the forum and over the internet, but i didnt find any solution.

Can anyone help me with that ?


Hi and welcome to the forum…

I would advise you to generate a model and CRUD with Gii… and then inspect the generated code…

so that you get a grasp about how the filtering in CGridView works… mainly using the $model->search() function

If you need more info just ask here in the forum…

I would be better to start once again with the code genrated by Gii.

You miss all conditions, and also the filter sould be an active text field.

In the view:



            'filter' => CHtml::activeTextField($model, 'nome_vida'), 

            'value' => '$data->vida->nome_vida',


In the model you should add:

public $nome_vida;



array('nome_vida', 'safe', 'on'=>'search')

// public function search()

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


All other parts you can leave as Gii generated.

Thanks zaccaria and mdomba

regenerating the model and the crud, and adding the code that zac has passed, the filter now is working.

This is a very clear step by step tutorial on how to make related tables Filter. work with CGridView .

Those who want it with dropdownlist should do like this:



            'filter'=>CHtml::listData(Model::model()->findAll(), 'nome_vida','nome_vida'),

            'value' => '$data->vida->nome_vida',


Hi all,

I have problem in filtering with dropDownList. I have 2 models Servicecall and JobStatus,

relation between these models is

‘jobStatus’ => array(self::BELONGS_TO, ‘JobStatus’, ‘job_status_id’),

My code in search() is,

$criteria->with = array( ‘customer’,‘jobStatus’);

$criteria->compare( ‘’, $this->job_status, true );

My code in admin view is,


  'filter'=> CHtml::listData(JobStatus::model()->findAll(), 'id', 'name'),



I am able to get the dropDown in view bur its not filtering.I am able to filter without dropDown, I am not able to figure out the mistake, Please help…

@zaccaria - thanks a lot! that’s what I need. I had the same problem and it works now. Thanks man!