php Doctrine2 分页器获得总结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15906051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Doctrine2 Paginator getting total results
提问by Eugene
Right away I would say, that I read this question Doctrine2 Paginator, but it doesn't give me enough information regarding the title of my question.
我马上会说,我读了这个问题Doctrine2 Paginator,但它没有给我足够的关于我的问题标题的信息。
When I used Doctrine1 I was getting the result with such code for example:
当我使用 Doctrine1 时,我得到了这样的代码的结果,例如:
$list = $this->query
->addSelect( 'SQL_CALC_FOUND_ROWS *' )
->offset( ( $this->currentPage - 1 ) * $this->perPage )
->limit( $this->perPage )
->execute( array(), \Doctrine_Core::HYDRATE_ARRAY_SHALLOW );
$totalRecords = SqlCalcFoundRowsEventListener::getFoundRowsCount();
$this->totalPages = ceil( $totalRecords / $this->perPage );
and it was great.
很棒。
Now, when using Doctrine2 I'm confused on how should I get total amount of records with same query as limited result for current page.
现在,当使用 Doctrine2 时,我很困惑我应该如何使用与当前页面的有限结果相同的查询获取记录总数。
Any help/advice would be greatly appreciated.
任何帮助/建议将不胜感激。
回答by Ocramius
The paginator usage is as following:
分页器用法如下:
$paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query);
$totalItems = count($paginator);
$pagesCount = ceil($totalItems / $pageSize);
// now get one page's items:
$paginator
->getQuery()
->setFirstResult($pageSize * ($currentPage-1)) // set the offset
->setMaxResults($pageSize); // set the limit
foreach ($paginator as $pageItem) {
echo "<li>" . $pageItem->getName() . "</li>";
}
回答by Sztukmistrz
Or some other loop:
或者其他一些循环:
for ($i=0; $i < $pagesCount; $i++) {
if ($currentPage == ($i+1)) {
$pagination1 .= '<li class="active"><a href="'.\URL::current().'?pagina='.($i+1).'" title="">'.($i+1).'</a></li>';
}else{
$pagination1 .= '<li><a href="'.\URL::current().'?pagina='.($i+1).'">'.($i+1).'</a></li>';
}
}