小编Mir*_*915的帖子

cakephp 3 中通过嵌套关联对查询结果进行排序

问题:

我有一个 cakephp 3.x 查询对象,带有两个嵌套关联 ,Organizations.Positions.Skills它被设置为视图变量 $Organizations。我试图按第一个嵌套关联中的列对查询生成的顶级数组进行排序。也就是说,我想$Organizations按 中的列排序Positions,特别是Positions.Ended)。

public function index()
{
    $this->loadModel('Organizations');
    $this->set('Organizations',
            $this->Organizations->find('all')
            ->contain([ //eager loading
            'Positions.Skills'
             ])
    );
}
Run Code Online (Sandbox Code Playgroud)

型号信息

组织有很多职位

职位有很多技能

我所做的研究

订购选项

根据说明书, find() 有一个顺序选项:find()->order(['field']);find('all', ['order'=>'field DESC']); 但是,这仅适用于调用 find() 的表中的字段。在这种情况下,组织。例如,这就是它的典型使用方式。

//This works.
//Sorts Organizations by the name of the organization in DESC.
$this->loadModel('Organizations');
$this->set('Organizations',
        $this->Organizations->find('all')
        ->contain([ //eager loading
            'Positions.Skills'
         ])
        ->order(['organization DESC'])
);
Run Code Online (Sandbox Code Playgroud)

但是,尝试将它用于嵌套关联是行不通的:

$this->set('Organizations',
  this->Organizations->find(...)
    ->contain(...)
    ->order(['Organizations.Positions.ended …
Run Code Online (Sandbox Code Playgroud)

php sorting cakephp associations cakephp-3.x

4
推荐指数
1
解决办法
4873
查看次数

标签 统计

associations ×1

cakephp ×1

cakephp-3.x ×1

php ×1

sorting ×1