cakephp - 按所包含模型中的字段排序

Pat*_*ess 3 php cakephp

我正在尝试获得特定类别的所有产品(产品包括并且属于Catrgories).我的条件是Category.name ='whatever'和Product.live = 1

我可以这样做:

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

但是我也想通过Product.sort_order对结果进行排序,所以我尝试了:

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

但这不起作用.这是正确的方法吗?

Dav*_*ave 6

TLDR: 使用联接而不是包含(或交换查找的方向)

我已经回答了 这个 问题, 6 8 堆栈溢出-不知道这是否只是不上来搜索 ...等还是什么,但是-你不能被包含的模型订单,因为通常含创建多个/单独的查询.