Fostah
Fostah

Reputation: 2946

Sorting related models in a Yii findByPk()

Using the Yii Framework, How can I sort the related model modifiervalues by "sortorder ASC, name ASC"? I have tried order->('modifiervalues.sortorder ASC, modifiervalues.name ASC') to no avail

<?php
    $item = Item::model()
    ->with("modifiergroups.modifiervalues")
    ->findByPk($id);
?>

Upvotes: 1

Views: 6835

Answers (2)

Onkar Janwa
Onkar Janwa

Reputation: 3950

<?php
    $item = Item::model()
    ->with(array('modifiergroups'=>array('order'=>'modifiervalues.sortorder ASC, modifiervalues.name ASC')))
    ->findByPk($id));
?>

You need to add base relation name in order also. Try this.

Upvotes: 0

adamors
adamors

Reputation: 2656

Try

$item = Item::model()
        ->with('modifiergroups')
        ->find(array(
                'condition'=>'id = :id',
                'params'=>array('id'=>$id),
                'order'=>'modifiervalues.sortorder ASC, modifiervalues.name ASC'
              )
         );

or (I haven't actually tried this one, but it should work just the same)

$item = Item::model()
        ->with(array('modifiergroups'=>array('order'=>'modifiervalues.sortorder ASC, modifiervalues.name ASC')))
        ->findByPk($id));

Notes:

  • if you give with only a field, I don't think you can order by another field. You should give with the name of the relation.
  • CActiveRecord doesn't have an order method

Upvotes: 3

Related Questions