Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii: Multiple order by conditions

Tags:

php

yii

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.

like image 218
Preetam Avatar asked Mar 25 '23 00:03

Preetam


1 Answers

$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

like image 102
javijuol Avatar answered Apr 13 '23 02:04

javijuol