根据所包含的模型中的字段进行排序


cakeorder by a field in the contained Model

我试图得到所有的产品是在一个特定的类别(产品hasandbelongstomany类别)。我的条件是Category.name='whatever'和Product。生活= 1

我可以这样做:

$cats = $this->Category->find('all', array(
    'conditions' => array(
        'Category.name' => $categoryName
    ),
    'contain' => 'Product.live = 1'
));

但是我还想按Product对结果进行排序。sort_order,所以我尝试:

$cats = $this->Category->find('all', array(
    'conditions' => array(
        'Category.name' => $categoryName
    ),
    'order' => array('Product.sort_order'),
    'contain' => 'Product.live = 1'
));

但这不起作用。正确的做法是什么?

TLDR:使用join而不是contains(或交换查找方向)

我已经在Stack Overflow上回答了这个问题大约68次了-不确定它是否只是没有出现在搜索中…等等,但是-你不能通过包含模型来订购,因为包含通常会创建多个/单独的查询。