Yii:按条件多重排序 [英] Yii: Multiple order by conditions

查看:39
本文介绍了Yii:按条件多重排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 CActiveDataprovider 来显示来自不同表的数据.现在我遇到了一个问题.我有两个表(项目和类别),它们都有 priority_order 列,我需要使用两列的 order 显示数据.

I am using CActiveDataprovider to show data from different tables. Now I am stuck with a problem. I have got two tables (items and categories) and they both have got priority_order columns and I need to show the data using order by both the columns.

例如:有两个类别和六个项目属于这些类别:

For example: there are two categories and six items belonging to these categories:

  • 食物(priority_order 1)
  1. food_item1(priority_order 1)
  2. fodd_item2 (priority_order 2)
  3. fodd_item3(priority_order 3)

  • 喝酒(priority_order 2)

    1. drink_item1(priority_order 1)
    2. drink_item2(priority_order 2)
    3. drink_item3(priority_order 3)

  • 现在我需要完全按照上面的顺序在 CGridView 中显示数据.所有的食物都会先出现,并按照它们的优先级排序,饮料会在后;显然是按照他们的顺序.

    Now I need to show the data in CGridView exactly as they are in above order. All the food items will come first and will be sorted by their priority_order and drink items will come later; obviously in their order.

    ItemsController 我正在尝试下面的代码(现在仅按类别排序)

    In ItemsController I am trying below code (for now order by categories only)

    $dataProvider = new CActiveDataProvider('Items', array(
                    'criteria' => array(
                        'with' => array('category'),
                        'condition' => 'user_id=' . Yii::app()->user->id,
                        //'order' => 't.priority_order ASC',
                        'order' => 'category.priority_order ASC',
                    ),
          ));
    

    如果还不够清楚,我很乐意提供更多细节.任何帮助将不胜感激.

    I would be happy to provide more details if still not clear enough. Any help would be appreciated.

    推荐答案

    $dataProvider = new CActiveDataProvider('Items', array(
                'criteria' => array(
                    'with' => array('category'),
                    'condition' => 'user_id=' . Yii::app()->user->id,
                    'order' => 'category.priority_order ASC, t.priority_order ASC',
                ),
      ));
    

    为订单标准提供第二个参数应该有效

    Giving a second param to the order criteria should work

    这篇关于Yii:按条件多重排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆