[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:




<?php 

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

	'id'=>'business-grid',

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

	'filter'=>$model,

	'columns'=>array(

		'id',

		array(              

	            'name'=>'business_type',

		    'header'=>'Business Type',

        	    'value'=>'$data->relationXYZ->nameABC',

		),	

?>



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

Tom

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:


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

to something like:




$criteria->compare('nameABC',$this->business_type);

$criteria->width('relatedTable');



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:


	

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



to:


		

$criteria->compare('nameABC',$this->business_type);

$criteria->width('relatedTable');



But I got error:

[color="#FF0000"][size="6"]CException[/size][/color]

[color="#FF0000"]Description

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:




<?php

private static $_items = array();

  

    public static function items(){

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

        return self::$_items;

    }

    public static function item($id_business_type){

        if(!isset(self::$_items[$id_business_type]))

            self::loadItems();

        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)

            self::$_items[$model->id]=$model->name_en;

    }

?>



Then in cGridView:




        array(             

            'name'=>'id_business_type',

            'header'=>'Business Type',

            'value'=>'$data->rel_id_business_type->name_en',

            '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.

Thanks

Tom

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