[SOLVED !!] Filtering in "Advanced search" fails for values from relation

Hi all,

Filtering values in [color="#006400"]zii.widgets.grid.CGridView[/color] doesn’t work for me, when I am trying to filter on the column that displays values from relation.

Fore example:

I filter here on column "Name" using "bph" and 1 row is found with "Bank BPH". The column name is from current model.

But here:

I filter on column "Business Type" using word "ba" which I expect to return all rows of "Banking", but I get nothing. The column "Business Type" displays data from relation to other model:


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








		    'header'=>'Business Type',




I suspect why is that… Values displayed on the view are from relation (for example "Banking"), but in the current model in the column business_type I have values 1000001, 1000002… etc

Any help how to fix this?

Thanks in advance


Anybody? ???

You have to change the function search() in the model, there is a line for this field that has to be changed.

From this:


to something like:



In order to have the right comparison. As it is now, it complare the id of the bank

Typo there - should be $criteria->with(‘relatedTable’);

I have changed in my model search() function from:







But I got error:



CDbCriteria does not have a method named "width".[/color]

I also tried to change to "with" but the same error appeared…

Checking the doc, looks like that with is not a method but a property.

you should:

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

Please, notice that my post are just advice, they can contain typos or mistake, refer to class reference in case of such errors

Well, the code

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

is now fine, but instead of ‘relatedTable’ i have to put ‘defined_relation’ from model - anyway still filtering on “ba” won’t show “Banking”.

What query does Yii generate? Can you post the sql?

Well finally I got it working with some help…

In DictBusinessTypes model (containing names like Banking, Accountancy, Real Estate - dictionary of business types…) I have this magic functions:


private static $_items = array();


    public static function items(){

        if(!count(self::$_items)) self::loadItems();

        return self::$_items;


    public static function item($id_business_type){



        return isset(self::$_items[$id_business_type])? self::$_items[$id_business_type] : false;


    public static function loadItems(){


        $models = self::model()->findAll();


        foreach($models as $model)




Then in cGridView:



            'header'=>'Business Type',


            'filter'=> DictBusinessTypes::items(),


So in the filter field I got now dropdown list:

and it filters appropriate column when values is chosen. My primary assumption was to have it as on default = input field in which I enter "ba" and it filters records of type "Banking" - anyway this is also fine.



Bump for showing the solution for you. Can I edit topics title to add [SOLVED] when solved? Anyone?

Thanks for this - appreciated very much.

You might want to have a look here

It’s a nice guide referring to relational data search, found in this forum discussion