简体   繁体   English

在原则1中从LEFT JOIN到leftJoin()-如何正确执行?

[英]LEFT JOIN to leftJoin() in Doctrine 1 - how to do it properly?

I have two tables, request and request_reply , here is the LEFT JOIN query that selects requests along with the numbers of replies: 我有两个表requestrequest_reply ,这是LEFT JOIN查询,它选择请求以及答复数:

SELECT request.id, COUNT(request_reply.id) as replies
FROM request LEFT JOIN request_reply ON request.id = request_reply.request_id
GROUP BY `request`.`id`
ORDER BY replies ASC

But when I am trying to write it as a Doctrine query: 但是,当我尝试将其编写为“学说”查询时:

$query = Doctrine_Query::create()
         ->select('Request.id, COUNT(RequestReply.id) as reply_count')
         ->from('Request')
         ->leftJoin('RequestReply ON Request.id = RequestReply.request_id')
         ->groupBy('Request.id')
         ->orderBy('reply_count ASC, Request.id DESC');

It comes out as without LEFT JOIN : 它像没有LEFT JOIN

SELECT `r`.`id` AS `r__id`, COUNT(`r2`.`id`) AS `r2__0` 
FROM `request` `r`, `request_reply` `r2`
GROUP BY `r`.`id`
ORDER BY `r2__0` ASC, `r`.`id` DESC

I don't understand what is wrong, I have tried everything, but there is no JOIN in the resulting query no matter what I do. 我不明白这是什么问题,我已经尝试了所有方法,但是无论执行什么操作,结果查询中都没有JOIN And this is a legacy project with Doctrine 1, so I can't update to Doctrine 2. 这是Doctrine 1的遗留项目,因此我无法更新到Doctrine 2。

Update 更新资料

Mappings. 映射。

Request.php

$this->hasMany('RequestReply as Replies', array('local' => 'id', 'foreign' => 'request_id'));

RequestReply.php

$this->hasOne('Request', array('local' => 'request_id', 'foreign' => 'id'));

have you mapped your entities correctly? 您是否正确映射了实体? I'd imagine that in your Entity Request you have a OneToMany annotation or anything else (YAML..) that points to the Entity RequestReply. 我想像一下,在您的实体请求中,您将具有一个OneToMany注释或指向实体RequestReply的其他任何内容(YAML ..)。 If so, you should be able to achieve what you are looking for just doing this: 如果是这样,那么您只需执行以下操作即可实现所需的功能:

$query = Doctrine_Query::create()
         ->select('Request.id, COUNT(rp.id) as reply_count')
         ->from('Request r')
         ->leftJoin('r.RequestReply rp')
         ->groupBy('r.id')
         ->orderBy('reply_count ASC, r.id DESC');

Let'me know if you got it working. 让我知道您是否可以正常使用。 Any doubt with the association mapping you can find here 对关联映射有任何疑问,您可以在这里找到

[EDIT] [编辑]

Nick, Try again please. 尼克,请再试一次。 The first error was because I didn't know you had use a Alias Replies. 第一个错误是因为我不知道您使用了Alias Replies。 Sorted out. 挑出来。 The second error is gonna be gone with the field r.id in the SELECT. 第二个错误将与SELECT中的字段r.id消失。 ;-) ;-)

$query = Doctrine_Query::create()
         ->select('r.id, COUNT(rp.id) as reply_count')
         ->from('Request r')
         ->leftJoin('r.Replies rp')
         ->groupBy('r.id')
         ->orderBy('reply_count ASC, r.id DESC');

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

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