通过关联计数来命令SQL Join

时间:2012-05-08 09:18:46

标签: php sql doctrine-orm dql

我有一个像这样的DQL查询:

$dql = "SELECT p, count(u.id) as ucnt FROM IAA\Entity\Project p " .
       "JOIN p.likedByUsers u ORDER BY ucnt";
$query = $this->_em->createQuery($dql);

问题是,我怎样才能坚持按计数排序,但不能得到结果?我想只获得按计数排序的项目,而不是使用[project]和[ucnt]排序的项目。

2 个答案:

答案 0 :(得分:5)

从Doctrine 2.2 (source)开始:

SELECT a, COUNT(b.id) AS HIDDEN c [...] ORDER BY c

答案 1 :(得分:-1)

尝试:

$dql = "SELECT p FROM IAA\Entity\Project p " .
       "JOIN p.likedByUsers u ORDER BY count(u.id)";
$query = $this->_em->createQuery($dql);