8

I cant find Doctrine_Pager in Doctrine2 and really need a way to page my query results. Is there a way to use some alternative pager (Pear, Zend)? Please post some example code as well if solution is available. Google didnt helped me, so hope folks will :)

pentarim
  • 463
  • 1
  • 6
  • 18

5 Answers5

6

I wrote this extension for Doctrine2 that contains a powerful pager:

http://github.com/beberlei/DoctrineExtensions

beberlei
  • 4,297
  • 1
  • 23
  • 25
  • The code looks really nice, cant wait to try it out, thanks – pentarim Aug 21 '10 at 16:08
  • @timdev, could u give a sample code, how we use Zend_Paginator and DoctrineExtensions\Paginate\PaginationAdapter or DoctrineExtensions\Paginate\Paginate ??? – ulduz114 Sep 12 '11 at 07:08
2

In Doctrine 2.2, there is a Paginator helper class: http://docs.doctrine-project.org/en/latest/tutorials/pagination.html

Very easy to use :)

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";
}
Vincent Pazeller
  • 1,448
  • 18
  • 28
1

PagerFanta, as mentioned by Maksim, comes recommended. However, there is also one other pagination bundle that should at least be mentioned: http://symfony2bundles.org/knplabs/KnpPaginatorBundle

Unlike PagerFanta, KnpPaginatorBundle currently requires the zend paginator package as a dependency.

mdpatrick
  • 1,082
  • 9
  • 12
0

Just as a note though, Query#setMaxResults / Query#setFirstResult covers most of the basic needs for paging.

romanb
  • 6,391
  • 2
  • 20
  • 19
-1

There is a good 3rd one solution: https://github.com/whiteoctober/Pagerfanta

I found it very useful

Maksim Kotlyar
  • 3,821
  • 27
  • 31