Gridview Filtering For Relational Model

hallo everyone,

I have 3 models each one have relation with others, I want to configure the filter for that in GridView

this is the models:

serviceCenter Model:




public function relations()

	{

		return array(

			'ownerData' => array(self::BELONGS_TO, 'Ownerdata', 'ownerData_id'),

        }

OwnerData Model :




public function relations()

	{

		return array(

			'provinceData' => array(self::BELONGS_TO, 'Provincedata', 'ProvinceData_id'),

		);

	}

and Provincedata Model has these columns

integer $id

string $provinceName

now what i have to do, is display the province name from ServiceCenter admin page with filter on it.

the grid view in admin page:


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

		'id'=>'my-grid',

		'dataProvider'=>$servic->searchEX(),

		'filter'=>$servic,

		'columns'=>array(

		'id',

		 array( 

		 	'name' => 'ownerData.name',

			'filter' => CHtml::activeTextField($servic->searchName, 'name'),

		 ),

		array(

			'name'=>'ownerData.ProvinceData_id',

		),

               

                ....



Note: in the ownerData.name the filter work correctly

check this out

http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/

thank you,

but this work with tow models and relation between its,

in my case I have

— Model A <–> Model B relation

— Model B <–> Model C relation

And in admin page (Model A) I need filtering the data which is at the (Model C)

Hi MoHaMmAd_K, welcome to the forum.

It’s almost the same … just add the relation of “ownerData.proviceData” to “with”. :)




$criteria = new CDbCriteria;

$criteria->with = array( 'ownerData', 'ownerData.provinceData' );

...

$criteria->compare( 'ownerData.name', $this->searchName, true );

$criteria->compare( 'provinceData.provinceName', $this->searchProviceName, true );

...



http://www.yiiframework.com/doc/guide/1.1/en/database.arr#performing-relational-query

I have the same problem

where you define "searchName" ?

because I will return this error:




PHP notice


Undefined variable: searchName



thanks

@m@ster

Did you read the link that alirz23 has suggested?

read and Problem Solved :rolleyes:

thanks

I just released a behavior to help with this: Related Search Behavior