关于Yii中CGridView关联表字段的filter问题解决方法
2014-07-12 00:00
399 查看
摘要: 关于Yii中CGridView关联表字段的filter问题解决方法
当你想用CGridView控件来生成一个Grid表格的时候,是非常方便的,你只需要简单的指定几个属性就可以了,比如:
$this->widget('zii.widgets.CGridView', array(
'id'=>'order-grid-view',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'customer_cd'
),
)
问题是,当你需要多多表进行查询的时候,filter就需要多作一点工作了,很遗憾,我没有找到Yii官方对此问题的简易方法,所以,只能靠迂回的方式了。
首先,让关联表字段显示在Grid表格里面很容易,你可以在dataProvider属性加上关联表,然后columns属性里直接加上要显示的关联表属性就可以,比如,修改后的dataProvider为
'dataProvider'=>$model->with('relationNameToOtherTable')->search(),
修改后的columns属性为:
'columns'=>array(
'id',
'customer_cd',
'customer_nm'=>array(
'name'=>'relationNameToOtherTable.customer_nm',
),
),
但这时的customer_nm是不具备filter域的
解决方法如下:(修改主表对应的Model类)
1)手动为主表增加关联表属性,public $this->customer_nm;
2) 修改search()方法,增加如下两行:$criteria->with = 'relationNameToOtherTable';//relationNameToOtherTable是你在relations里面定义的键值
$criteria->compare('customer_nm', $this->customer_nm);
3)增加afterFind()方法到主表的model类,方法内容如下:$this->customer_nm = $this->relationNameToOtherTable->customer_nm;
这时,你就可以不像前面写的那样修改dataProvider和columns了,因为你手动增加的那个属性已经把关联表的字段属性加进来了,直接用就可以了,最终view代码如下:
'dataProvider'=>$model->search();
'columns'=>array(
'id',
'customer_cd',
'customer_nm'=>array(
'name'=>'relationNameToOtherTable.customer_nm',
),
),
ok, that's all, not the best solution, but it works any way, well if you have some good ideas for this kind of problem feel free to tell me, thanks a lot!!
当你想用CGridView控件来生成一个Grid表格的时候,是非常方便的,你只需要简单的指定几个属性就可以了,比如:
$this->widget('zii.widgets.CGridView', array(
'id'=>'order-grid-view',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'customer_cd'
),
)
问题是,当你需要多多表进行查询的时候,filter就需要多作一点工作了,很遗憾,我没有找到Yii官方对此问题的简易方法,所以,只能靠迂回的方式了。
首先,让关联表字段显示在Grid表格里面很容易,你可以在dataProvider属性加上关联表,然后columns属性里直接加上要显示的关联表属性就可以,比如,修改后的dataProvider为
'dataProvider'=>$model->with('relationNameToOtherTable')->search(),
修改后的columns属性为:
'columns'=>array(
'id',
'customer_cd',
'customer_nm'=>array(
'name'=>'relationNameToOtherTable.customer_nm',
),
),
但这时的customer_nm是不具备filter域的
解决方法如下:(修改主表对应的Model类)
1)手动为主表增加关联表属性,public $this->customer_nm;
2) 修改search()方法,增加如下两行:$criteria->with = 'relationNameToOtherTable';//relationNameToOtherTable是你在relations里面定义的键值
$criteria->compare('customer_nm', $this->customer_nm);
3)增加afterFind()方法到主表的model类,方法内容如下:$this->customer_nm = $this->relationNameToOtherTable->customer_nm;
这时,你就可以不像前面写的那样修改dataProvider和columns了,因为你手动增加的那个属性已经把关联表的字段属性加进来了,直接用就可以了,最终view代码如下:
'dataProvider'=>$model->search();
'columns'=>array(
'id',
'customer_cd',
'customer_nm'=>array(
'name'=>'relationNameToOtherTable.customer_nm',
),
),
ok, that's all, not the best solution, but it works any way, well if you have some good ideas for this kind of problem feel free to tell me, thanks a lot!!
相关文章推荐
- 关于Yii中CGridView关联表字段的filter问题解决方法
- 关于oracle中clob字段查询慢的问题及解决方法
- 关于删除数据库中含有自增长字段的表,清空后,自增长字段重新从1开始的问题解决方法。
- Yii框架常见问题: CGridView关联表字段的filter问题解决
- c#关于int(或其他类型)的字段在对象初始化时默认初始化问题的解决方法
- 关于含有标识字段的数据表中记录的插入问题解决(IDENTITY_INSERT)
- 关于DateTime在Pocket PC客户端不能正确存入数据库的问题及解决方法
- 关于informix的256字符限制问题解决方法
- 关于SQL的几个问题的解决方法
- 关于在使用Exchange2003系统时无法向sina,yahoo,hotmail等邮箱发送邮件问题的解决方法
- 关于Eclipse RCP的两个小问题和解决方法
- 关于RUNDLL32.exe占用大量CPU的问题以及解决方法
- 关于IE出现问题的解决方法
- 关于数据库对象所有者非dbo时的可能造成的问题及解决方法
- 关于使用VS.Net2003调试器出现的问题及相关解决方法
- 关于流和缓冲区的理解以及一般标准输入问题的解决方法小结.
- 关于"无法定位程序输入点 RtlUnhandledExceptionFilter 于动态链接库"解决方法
- DataGuard - 一个关于Physical Standby中recover问题的解决方法
- 关于水晶报表中CrystalDecisions.CrystalReports.Engine.LogOnException问题的解决方法
- 关于DIV被SELECT遮挡问题的解决方法