Yii Framework - CGridView 对相关列进行排序

发布于 2024-11-09 02:32:58 字数 1260 浏览 0 评论 0原文

预先感谢任何可以提供帮助的人。我一直在寻找答案,但还没有找到。我遇到过从一行到重写整个类都不起作用的“解决方案”。

我有“网格”来显示关系,并且能够使用搜索功能。我不明白的是排序功能。进行以下更改后,列标题将变得不可单击。

这就是我所拥有的:

关系名称/标签是“公司”,在员工模型中设置。

表:员工 -- 列: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黄昏下泛黄的笔记 2024-11-16 02:32:58

我也遇到了同样的问题,最后是这样解决的:

模型搜索方法:

$sort = new CSort();
$sort->attributes = array(
'assignedTo'=>array(
    'asc'=>'(SELECT surname from people 
            WHERE people.person_id = t.assigned_to) ASC',       
    'desc'=>'(SELECT surname from people 
            WHERE people.person_id = t.assigned_to) DESC',     
    ),
    '*', // add all of the other columns as sortable   
); 

查看文件

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tasks-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
    'task',
    array(
    'header'=>'Assigned To',
    'value'=> '$data->assignedTo->surname.", ".$data->assignedTo->forename',
        'name'=> 'assignedTo',
        'sortable'=>TRUE,
        ),
    'due_date',
    'status',       
),

:));

这样,我从相关表中选择单个字段到 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:

$sort = new CSort();
$sort->attributes = array(
'assignedTo'=>array(
    'asc'=>'(SELECT surname from people 
            WHERE people.person_id = t.assigned_to) ASC',       
    'desc'=>'(SELECT surname from people 
            WHERE people.person_id = t.assigned_to) DESC',     
    ),
    '*', // add all of the other columns as sortable   
); 

View file:

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tasks-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
    'task',
    array(
    'header'=>'Assigned To',
    'value'=> '$data->assignedTo->surname.", ".$data->assignedTo->forename',
        'name'=> 'assignedTo',
        'sortable'=>TRUE,
        ),
    'due_date',
    'status',       
),

));

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!

转瞬即逝 2024-11-16 02:32:58

这似乎是 [yii] 上的一个日常问题。将这些内容从搜索功能中删除,然后将过滤器属性添加到 CGridView 列中,如下所示:

        array(
                'name'=>'company',   
                'value'=>'$data->company->companyNick',
                'filter' => CHtml::listData(Company::model()->findAll(),'id','nick'),
        ),

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:

        array(
                'name'=>'company',   
                'value'=>'$data->company->companyNick',
                'filter' => CHtml::listData(Company::model()->findAll(),'id','nick'),
        ),
烟酒忠诚 2024-11-16 02:32:58

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/

兔小萌 2024-11-16 02:32:58

我喜欢迈克·H 的回答。我还想指出,您可以使用 with() 执行关系查询,然后将 select 设置为 false 以防止实际加载相关模型,而不是输入原始 SQL。您还可以通过传递点符号字符串来在视图中使用相关属性的 attributeLabel,例如:


// Controller
$gridDataProvider = new CActiveDataProvider('EmrFormPatientTie', array(
    'criteria'=>array(
        'condition'=>'flag_locked=0',
        'with'=>array('patient'=>array(
            'select'=>false, // Perform relational query without loading related models
        )),
    ),
    'pagination'=>(array('pageSize'=>15)),
    'sort'=>array(
        'attributes'=>array(
            'patient.display_id'=>array(
                'asc'=>'patient.display_id',
                'desc'=>'patient.display_id DESC',
            ),
            '*', // Make all other columns sortable, too
        ),
    ),
));

// View
array(
    'name'=>'patient.display_id',
    'value'=>'$data->patient->display_id',
),

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.,:


// Controller
$gridDataProvider = new CActiveDataProvider('EmrFormPatientTie', array(
    'criteria'=>array(
        'condition'=>'flag_locked=0',
        'with'=>array('patient'=>array(
            'select'=>false, // Perform relational query without loading related models
        )),
    ),
    'pagination'=>(array('pageSize'=>15)),
    'sort'=>array(
        'attributes'=>array(
            'patient.display_id'=>array(
                'asc'=>'patient.display_id',
                'desc'=>'patient.display_id DESC',
            ),
            '*', // Make all other columns sortable, too
        ),
    ),
));

// View
array(
    'name'=>'patient.display_id',
    'value'=>'$data->patient->display_id',
),

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文