Search code examples
phpcakephpmodelassociations

Cakephp, ordering associated tables


When I search a model which "has many" of something else.

For example a blog post has many categories.

When searching for blog post with categories associated, how do I order the associated categories? When the array is returned it ignores the order on the category model and defaults to it's usual id order.

Cheers.


Solution

  • In addition you can set the order in your model's relation.

    <?php
    class Post extends AppModel {
      var $hasMany = array(
        'Category' => array(
            'className' => 'Category',
            ...
            'order' => 'Category.name DESC',
            ....
        ),
    }?>