Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to page Doctrine2 results [closed]

Tags:

doctrine

pager

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 :)

like image 769
pentarim Avatar asked Aug 16 '10 19:08

pentarim


3 Answers

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

http://github.com/beberlei/DoctrineExtensions

like image 64
beberlei Avatar answered Nov 02 '22 23:11

beberlei


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";
}
like image 21
Vincent Pazeller Avatar answered Nov 03 '22 01:11

Vincent Pazeller


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.

like image 35
mdpatrick Avatar answered Nov 03 '22 00:11

mdpatrick