Vikas Kumar
Vikas Kumar

Reputation: 31

Sorting in yii 1.1

I have question regarding yii 1.1 sort.
I have three tables ticket, repairLogs and part table .
Following relations have been defined in Ticket model.

'repairLogs' => array(self::HAS_MANY, 'RepairLog', 'ticket_id', 'order'=>'ts DESC'),

and in repairLogs Table

'part' => array(self::BELONGS_TO, 'Part', 'part_id'),

Part table has a column 'number' and I want to sort the data based on "number". Can anyone guide me how to do this since I am new to the yii 1.1 framework.

Upvotes: 1

Views: 672

Answers (1)

benomite
benomite

Reputation: 868

You can do this during the find()

Ticket::model()->with(array('part', 'repairLogs'))->findAll(
    array(
        // your conditions here
        'order' => 'part.number DESC',   // "part" is the alias defined in your relation array (in the Ticket model file)
        'limit' => 10,
    )
);

If you are using A dataProvider, you can set it as a default order:

new CActiveDataProvider('Ticket', array(
    'criteria' => $criteria,    // you criteria that should include the "with" part
    'sort' => array(
        'defaultOrder' => 'part.number DESC',
    )
));

Upvotes: 4

Related Questions