1

我使用此 DQL 获取一些数据,但在使用教义 2 分页器时出现错误:

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

这是因为教义执行这个查询来计算页数:

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
4

1 回答 1

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
  )
于 2012-11-18T14:14:39.683 回答