group by的doctrine2 paginator错误

时间:2012-11-18 13:30:07

标签: symfony doctrine-orm dql

我使用这个DQL获取一些数据但是在使用doctrine2 paginator时出错:

SELECT f FROM DankeForumBundle:Forumusermessage f
  WHERE f.recipientdelete=0
  GROUP BY f.forwarder
  HAVING f.recipient=:recipient

这是因为doctrine执行此查询来计算页面:

SELECT count(DISTINCT f.id) FROM Forumusermessage AS f
  WHERE f.recipientdelete=0
  GROUP BY f.forwarder_id
  HAVING f.recipient_id=:recipient

有没有办法在不使用本机SQL查询的情况下对其进行分页?

编辑:

这是一个示例PHP代码:

$query = $this->getEntityManager()->createQuery('SELECT f FROM DankeForumBundle:Forumusermessage f
  WHERE f.recipientdelete=0
  GROUP BY f.forwarder
  HAVING f.recipient=:recipient');
$query->setParameter('recipient', $user);
$query->setFirstResult(0);
$query->setMaxResults(10);
$paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query);
echo count($paginator); // wrong page count

1 个答案:

答案 0 :(得分:0)

我找到了一个使用子查询的解决方法:

SELECT f FROM DankeForumBundle:Forumusermessage f
WHERE
  f.recipientdelete=0 AND
  f.recipient=:recipient
  AND f.id IN (
    SELECT fn FROM DankeForumBundle:Forumusermessage fn
    GROUP BY fn.forwarder
  )