I have a CGridView which includes a table column that is linked through the relations method of the model
'cat'=>array(self::BELONGS_TO, 'Cat', 'cat_id','joinType'=>'INNER JOIN'),
I want to make this column searchable, but am not sure how to adapt the search method to do this. How do I access the related column? Currently I have:
$criteria=new CDbCriteria;
$criteria->with = array('cat');
...
$criteria->compare('cat.name',<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />???);
The view code simply includes ‘cat.name’, as a column to display the column.
Alex_Muir
(Alex Muir)
October 17, 2010, 3:03pm
2
Are you stuck getting a text field to display in the filter element of CGridView?
I create extra attributes in the model eg.
class Thing extends CActiveRecordModel {
public $searchCategoryName;
public function rules ()
{
return array(
'...,searchCategoryName,... ','safe','on'=>'search'
);
}
public function search ()
{
$criteria=new CDbCriteria;
$criteria->with = array('cat');
...
$criteria->compare('cat.name',$this->searchCategoryName);
}
}
Thanks Alex
That got me almost there.
I just had to change my view to include this for the relevant column
array(
'name'=>'searchCategoryName',
'value'=>'$data->cat->name',
),