简体   繁体   English

在 CakePHP 3 中包含连接后加入子查询

[英]Join sub-query after contain joins in CakePHP 3

I am currently experiencing an issue in CakePHP 3 - I am trying to join a subquery onto an existing query, however the ORM is adding the subquery BEFORE processing the "contain" method, but the subquery join relies on some of the contained tables.我目前在 CakePHP 3 中遇到问题 - 我正在尝试将子查询加入现有查询,但是 ORM 在处理“包含”方法之前添加子查询,但子查询连接依赖于一些包含的表。 Is there any way to force the joins to run after contain?有什么方法可以强制连接在包含后运行?

My code is fairly long and convoluted but I will write a simple example:我的代码相当长且令人费解,但我将编写一个简单的示例:

$articles = $this->find()
  ->select(['Articles.id', 'Articles.title', 'Users.id'])
  ->contain(['Users','Tags']);

$subQuery = $this->find()
  ->select(['text' => 'Articles.body', 'user_id' => 'Users.id', 'article_id' => 'Articles.id'])
  ->contain(['Users','Tags']);

$articles
  ->join([
    'ArticleText' => [
      'table' => $subQuery,
      'type' => 'LEFT',
      'conditions' => ['ArticleText.user_id = Users.id', 'ArticleText.article_id = Articles.id']
    ]);

Now this code is overly simple and obviously unnecessary, but the issue is essentially that the ORM will generate a query that tries to perform the join first, and the join will fail because Users.id has not been joined yet.现在这段代码过于简单,显然没有必要,但问题本质上是 ORM 将生成一个尝试首先执行连接的查询,连接将失败,因为尚未连接 Users.id。 Is there any way to make the contains run first?有没有办法让包含先运行?

AFAIK the order cannot (yet) be influenced, containments and matchings (that's also the *JoinWith() methods) will come after manual joins, see also https://github.com/cakephp/cakephp/issues/10746 . AFAIK 订单不能(还)受到影响,包含和匹配(这也是*JoinWith()方法)将在手动加入后出现,另请参见https://github.com/cakephp/cakephp/issues/10746

So you'll probably have to join the Users association manually too, instead of using contain() .因此,您可能也必须手动加入Users关联,而不是使用contain()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM