如何在CakePHP 3中正确执行左连接?

如何在CakePHP 3中正确执行左连接?,php,mysql,cakephp,Php,Mysql,Cakephp,我一直在尝试使用CakePHP3的查询生成器格式来学习如何更好地使用它。然而,目前我似乎无法得到一个左连接工作 我希望在结果中收集两个表中的可用信息: $query = $this->find()->where(['userID' => $UID])->hydrate(false)->join([ 'table' => 'authgroups', 'alias' => 'a', 'type' =&g

我一直在尝试使用CakePHP3的查询生成器格式来学习如何更好地使用它。然而,目前我似乎无法得到一个左连接工作

我希望在结果中收集两个表中的可用信息:

    $query = $this->find()->where(['userID' => $UID])->hydrate(false)->join([
        'table' => 'authgroups',
        'alias' => 'a',
        'type' => 'LEFT',
        'conditions' => 'a.id = userGroup'
    ]);

    return $query->toArray();
如果我正确地阅读了这本食谱,这应该是可行的——但是,它被用于一个与示例不同的模型中,结果只从该表返回,似乎忽略了连接。我是否需要在控制器中执行此操作

这在模型中有效:

我想您的连接查询可能工作得很好。cakephp连接中重要的一点是:

If you are joining table 'A' with table 'B' from table 'A' ,
By default it will select all fields from table A and no fields from table B.
您还需要从另一个表中选择一些字段:

$query = $this->find()->where(['userID' => $UID])->hydrate(false)->join([
    'table' => 'authgroups',
    'alias' => 'a',
    'type' => 'LEFT',
    'conditions' => 'a.id = userGroup'
 ])->autoFields(true) // selecting all fields from current table
   ->select(["a.field1","a.field2"]); // selecting some fields from table authgroups

return $query->toArray();
}
我不知道这些东西在cakephp文档中是否提供得很好。

这在模型中有效:

我想您的连接查询可能工作得很好。cakephp连接中重要的一点是:

If you are joining table 'A' with table 'B' from table 'A' ,
By default it will select all fields from table A and no fields from table B.
您还需要从另一个表中选择一些字段:

$query = $this->find()->where(['userID' => $UID])->hydrate(false)->join([
    'table' => 'authgroups',
    'alias' => 'a',
    'type' => 'LEFT',
    'conditions' => 'a.id = userGroup'
 ])->autoFields(true) // selecting all fields from current table
   ->select(["a.field1","a.field2"]); // selecting some fields from table authgroups

return $query->toArray();
}

我不知道这些东西在cakephp文档中是否提供得很好。

恐怕这会返回未知的方法:字段-将其添加到选项数组中也没有效果。显然字段已被select替换,“正在”工作。当然应该选择它,因为它是cakephp 3。我怎么会错过它。它工作-它返回嵌套数据,但我可以解析它。恐怕这是返回未知方法:字段-将其添加到选项数组中也没有效果。显然字段已被select替换,哪一个“正在”工作。当然应该选择它,因为它是cakephp 3。我怎么会错过它。它工作-它返回嵌套数据,但我可以解析它。