CGridView filter with relational field [Solved]

Model:
1. defined filter property in model

var $a = "";
var $b = "";

2. configure rules for properties

array('......., a, b', 'safe', 'on'=>'search');

3. define relation in function relations()

public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'xxx' => array(self::HAS_ONE,  'xxxModel', array('...' => '....')),
);
}

4. add codes in function search()

$criteria->compare('xxx.ax',$this->a);
$criteria->compare('xxx.bx',$this->b);

View

'dataProvider'=>$model->with("xxx")->search(),

2. define column display form

array(
'name' => 'a',
'value' => '$data->xxx->ax',
'filter' => array("$array_for_drop_down_list"),
),
array(
'name' => 'b',
'value' => '$data->xxx->bx',
'filter' => array(1 => "Set", 0 => 'Not Set'),
),

Code hint:
1. The two properties are only used to store the search condition from UI form.
2. The CDbCriteria is not changed in function search().

Leave a Reply