【发布时间】:2011-03-30 15:14:17
【问题描述】:
我在 Doctrine2 中找不到 Doctrine_Pager,我真的需要一种方法来分页我的查询结果。有没有办法使用一些替代寻呼机(Pear,Zend)?如果解决方案可用,请发布一些示例代码。谷歌没有帮助我,所以希望人们会:)
【问题讨论】:
我在 Doctrine2 中找不到 Doctrine_Pager,我真的需要一种方法来分页我的查询结果。有没有办法使用一些替代寻呼机(Pear,Zend)?如果解决方案可用,请发布一些示例代码。谷歌没有帮助我,所以希望人们会:)
【问题讨论】:
在 Doctrine 2.2 中,有一个 Paginator 助手类:http://docs.doctrine-project.org/en/latest/tutorials/pagination.html
非常容易使用:)
use Doctrine\ORM\Tools\Pagination\Paginator;
$dql = "SELECT p, c FROM BlogPost p JOIN p.comments c";
//build the query for the page you want to display
$query = $entityManager->createQuery($dql)
->setFirstResult(0)
->setMaxResults(10);
$paginator = new Paginator($query, $fetchJoinCollection = true);
$c = count($paginator); //automatically makes another query and returns the total number of elements.
//iterating over the paginator iterates over the current page
foreach ($paginator as $post) {
echo $post->getHeadline() . "\n";
}
【讨论】:
推荐使用 Maksim 提到的 PagerFanta。但是,还有一个至少应该提到的分页包:http://symfony2bundles.org/knplabs/KnpPaginatorBundle
与 PagerFanta 不同,KnpPaginatorBundle 目前需要 zend 分页器包作为依赖项。
【讨论】:
有一个很好的第三个解决方案:https://github.com/whiteoctober/Pagerfanta
我觉得很有用
【讨论】:
请注意,Query#setMaxResults / Query#setFirstResult 涵盖了分页的大部分基本需求。
【讨论】:
我为包含强大寻呼机的 Doctrine2 编写了这个扩展:
【讨论】: