首页 > 代码库 > Yii框架常见问题: CGridView关联表字段的filter问题解决
Yii框架常见问题: CGridView关联表字段的filter问题解决
Model:
1. 添加filter用的属性
var $a = “”;
var $b = “”;
2. 修改rules方法中的配置
array(‘……., a, b’, ‘safe’, ‘on’=>’search’);
3. 注意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. 修改search()方法添加行
$criteria->compare(‘xxx.ax‘,$this->a);
$criteria->compare(‘xxx.bx‘,$this->b);
View
1. ‘dataProvider’=>$model->with(“xxx“)->search(),
2. 列展示修改
array(
‘name’ => ‘a‘,
‘value’ => ‘$data->xxx->ax‘,
‘filter’ => array(select的下来菜单数组),
),
array(
‘name’ => ‘b‘,
‘value’ => ‘$data->xxx->bx‘,
‘filter’ => array(1 => “Set”, 0 => ‘Not Set’),
),
代码提示:
1. 添加的两个属性仅仅用于记录页面的搜索条件
2. search方法中的CDbCriteria搜索方法没有任何变化,请学习如何使用关联搜索
Yii框架常见问题: CGridView关联表字段的filter问题解决