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

How do I correctly execute a LEFT join in CakePHP 3?

我一直在尝试使用 CakePHP 3 的查询生成器格式,以便学习如何更好地使用它。但是,目前我似乎无法让 left join 工作。

两个 table 中都有可用的信息,我想在结果中收集这些信息:

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

    return $query->toArray();

如果我正确阅读了 CookBook,这应该可以工作 - 但是,它在与示例不同的模型中使用,结果仅 returns 来自 table,似乎忽略了连接。我是否需要在控制器中执行此操作?

这适用于模型:

我猜你的连接查询可能有效 well.The 在 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.

您还需要 select 来自另一个 table 的一些字段:

$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文档中是否提供了这些东西。