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(







		 	'name' => '',

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







Note: in the the filter work correctly

check this out

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( '', $this->searchName, true );

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


I have the same problem

where you define "searchName" ?

because I will return this error:

PHP notice

Undefined variable: searchName



Did you read the link that alirz23 has suggested?

read and Problem Solved :rolleyes:


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