这是我的存储库函数的代码:
public function findLatest($count, $page)
{
return $this->getEntityManager()
->createQuery
('
SELECT
n.id, n.createdAt, n.title, n.titleCanonical, n.entry, n.movie,
c.title as category,
i as images,
COUNT(com.id) as numberOfComments
FROM WisJaCoreBundle:News n
JOIN n.category c
LEFT JOIN WisJaCoreBundle:Image i
WITH i.target = \'Article\' AND i.targetId = n.id AND n.movie IS NULL
LEFT JOIN WisJaCoreBundle:Comment com
WITH com.target = \'Article\' AND com.targetId = n.id
ORDER BY n.createdAt DESC
')
->setFirstResult($page * $count - $count)
->setMaxResults($count)
->getResult();
}
在表格新闻中我有一些记录,但我只得到一个......我发现了问题,这是这个片段
COUNT(com.id) as numberOfComments
当我将其替换为例如:
n.id as numberOfComments
然后一切顺利,我得到了所有结果。为什么“COUNT”会破坏我的查询?
EDIT ------------------------------------------
我仍然不知道为什么这个构造不起作用,但问题是在同一个SQL中,而只是“COUNT”放置了这样的嵌套查询:
SELECT
n.id, n.createdAt, n.title, n.titleCanonical, n.entry, n.movie,
c.title as category,
i as images,
(SELECT COUNT(com.id) FROM WisJaCoreBundle:Comment com WHERE com.target = \'Article\' AND com.targetId = n.id) as numberOfComments
FROM WisJaCoreBundle:News n
JOIN n.category c
LEFT JOIN WisJaCoreBundle:Image i
WITH i.target = \'Article\' AND i.targetId = n.id AND n.movie IS NULL
ORDER BY n.createdAt DESC
所以,问题解决了:)