我有两个表,request
这里request_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
但是当我尝试将其编写为 Doctrine 查询时:
$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');
结果没有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
我不明白出了什么问题,我已经尝试了所有方法,但是JOIN
无论我做什么,结果查询中都没有。这是一个带有 Doctrine 1 的遗留项目,所以我无法更新到 Doctrine 2。
更新
映射。
Request.php
$this->hasMany('RequestReply as Replies', array('local' => 'id', 'foreign' => 'request_id'));
RequestReply.php
$this->hasOne('Request', array('local' => 'request_id', 'foreign' => 'id'));