4
votes

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:

  • Food (priority_order 1)
    1. food_item1 (priority_order 1)
    2. fodd_item2 (priority_order 2)
    3. fodd_item3 (priority_order 3)
  • Drink (priority_order 2)
    1. drink_item1 (priority_order 1)
    2. drink_item2 (priority_order 2)
    3. drink_item3 (priority_order 3)

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.

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.

1
Why don't you just give second order by items ?Rikesh
Does it work like that?? Wont that replace the first order statement and will order the dataprovider by items only??Preetam
No it won't. At least have a go.Rikesh
sorry. that did work. was thinking it in different wayPreetam

1 Answers

7
votes
$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