pc131
(Skoczy)
October 28, 2010, 8:33pm
1
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
zaccaria
(Matteo Falsitta)
October 29, 2010, 10:08am
3
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
GSTAR
(Omzy83)
October 29, 2010, 10:55am
4
zaccaria:
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’);
pc131
(Skoczy)
October 29, 2010, 10:57am
5
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…
zaccaria
(Matteo Falsitta)
October 29, 2010, 11:01am
6
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
pc131
(Skoczy)
October 29, 2010, 11:18am
7
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”.
zaccaria
(Matteo Falsitta)
October 29, 2010, 12:22pm
8
What query does Yii generate? Can you post the sql?
pc131
(Skoczy)
October 29, 2010, 1:05pm
9
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
pc131
(Skoczy)
November 2, 2010, 7:55am
10
Bump for showing the solution for you. Can I edit topics title to add [SOLVED] when solved? Anyone?
MtlMike
(Mjswyers)
July 31, 2011, 2:42am
11
Thanks for this - appreciated very much.
dimvic7
(Dimitris Vic)
July 31, 2011, 3:22am
12
You might want to have a look here
It’s a nice guide referring to relational data search, found in this forum discussion