当我搜索“有很多”其他东西的模型时。
例如,博客文章有很多类别。
在搜索与相关类别相关的博文时,如何订购相关类别?返回数组时,它会忽略类别模型上的顺序,并默认为通常的id顺序。
干杯。
答案 0 :(得分:23)
此外,您可以在模型的关系中设置顺序。
<?php
class Post extends AppModel {
var $hasMany = array(
'Category' => array(
'className' => 'Category',
...
'order' => 'Category.name DESC',
....
),
}?>
答案 1 :(得分:4)
<?php
class Post extends AppModel {
var $hasMany = array(
'Category' => array(
'className' => 'Category',
...
'sort' => 'Category.name DESC',
....
),
}?>
答案 2 :(得分:3)
您可以使用ContainableBehavior:
执行此操作$this->Post->find('all', array('contain' => array(
'Category' => array(
'order' => 'Category.created DESC'
)
)));
http://book.cakephp.org/view/1325/Containing-deeper-associations
答案 3 :(得分:0)
您可以指定order
方法参数的find
属性。否则,它将默认为最顶层/父模型的顺序。在您的情况下Category.id
。
答案 4 :(得分:0)
按关联模型中的列排序需要设置sortWhitelist
。
$this->paginate['order'] = [ 'Fees.date_incurred' => 'desc' ];
$this->paginate['sortWhitelist'] = ['Fees.date_incurred', 'Fees.amount'];
$this->paginate['limit'] = $this->paginate['maxLimit'] = 200;