Yii Framework - CGridView 对相关列进行排序
预先感谢任何可以提供帮助的人。我一直在寻找答案,但还没有找到。我遇到过从一行到重写整个类都不起作用的“解决方案”。
我有“网格”来显示关系,并且能够使用搜索功能。我不明白的是排序功能。进行以下更改后,列标题将变得不可单击。
这就是我所拥有的:
关系名称/标签是“公司”,在员工模型中设置。
表:员工 -- 列:idCompany & 表:公司 -- 列:companyNick
admin.php - 查看
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'employee-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'company',
'value'=>'$data->company->companyNick',
),
'lastName',
'firstName',
ETC...
Employee.php - 模型
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
//Company Relation Search
$criteria->compare('company.companyNick',$this->company,true);
$criteria->with='company';
//stock
$criteria->compare('idEmployee',$this->idEmployee,true);
$criteria->compare('idAccount',$this->idAccount,true);
ETC...
Thanks in advance to anyone who can help. I've been searching for an answer, but haven't found one yet. I've run into "solutions" that haven't worked that run from 1 line, to re-writing an entire class.
I've got the "grid" to show the relation, and am able to use the search feature. What I can't figure out is the sort feature. The column header becomes non-clickable once the below changes have been made.
This is what I have:
The relation name/label is "company," setup in Employee model.
Table: Employee -- Column: idCompany
&
Table: Company -- Column: companyNick
admin.php - VIEW
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'employee-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'company',
'value'=>'$data->company->companyNick',
),
'lastName',
'firstName',
ETC...
Employee.php - MODEL
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
//Company Relation Search
$criteria->compare('company.companyNick',$this->company,true);
$criteria->with='company';
//stock
$criteria->compare('idEmployee',$this->idEmployee,true);
$criteria->compare('idAccount',$this->idAccount,true);
ETC...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也遇到了同样的问题,最后是这样解决的:
模型搜索方法:
查看文件
:));
这样,我从相关表中选择单个字段到 order by 子句中,然后按该字段进行排序,您在表达式中创建表联接,在本例中为 -
people.person_id = t.assigned_to< /code> (其中 t 是 yii 提供的表别名)。这也许不是创建 order by 子句的最有效方法,但它确实有效!
I've been having the same problems and in the end solved it this way:
Model Search method:
View file:
));
This way I'm selecting a single field from the related table into the order by clause and then ordering by that, you create the table join in the expression, in this case it is -
people.person_id = t.assigned_to
(where t is a table alias provided by yii). This is perhaps not the most efficient way to create the order by clause but it works!这似乎是 [yii] 上的一个日常问题。将这些内容从搜索功能中删除,然后将过滤器属性添加到 CGridView 列中,如下所示:
This seems to be a daily question on [yii]. Strip that stuff out of your search function, and add a filter attribute to your CGridView column like so:
Yii 网站 Wiki 页面上也有很好的解释如何执行此操作:
http://www.yiiframework。 com/wiki/281/searching-and-sorting-by-lated-model-in-cgridview/
There is also good explanation how to do this on Yii web site Wiki page:
http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/
我喜欢迈克·H 的回答。我还想指出,您可以使用 with() 执行关系查询,然后将 select 设置为 false 以防止实际加载相关模型,而不是输入原始 SQL。您还可以通过传递点符号字符串来在视图中使用相关属性的 attributeLabel,例如:
I like Mike H's answer. I also want to point out that you could, instead of entering the raw SQL, use with() to perform a relational query and then set select to false to prevent actually loading the related models. You can also use the related attribute's attributeLabel in the view by passing the dot-notated string, e.g.,: