我使用此 DQL 获取一些数据,但在使用 doctrine2 分页器时出现错误:
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 DoctrineORMToolsPaginationPaginator($query);
echo count($paginator); // wrong page count
我找到了使用子查询的解决方法:
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
)