22

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.

When I used Doctrine1 I was getting the result with such code for example:

$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.

Any help/advice would be greatly appreciated.

Community
  • 1
  • 1
Eugene
  • 4,352
  • 8
  • 55
  • 79
  • 1
    Will eventually answer tomorrow - for now please check the tests for the paginator at https://github.com/doctrine/doctrine2/blob/master/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php – Ocramius Apr 09 '13 at 21:46
  • Looked at tests, but there are no tests, that set limitation to query before passing it to paginator. Maybe I misunderstood something. – Eugene Apr 09 '13 at 22:33

2 Answers2

54

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>";
}
ioleo
  • 4,697
  • 6
  • 48
  • 60
Ocramius
  • 25,171
  • 7
  • 103
  • 107
  • 9
    btw, you don't need to `count()` the `$paginator` BEFORE setting the limit and offset. You can set the limit and offset before initializing your paginator and it will still work as expected. – Ramy Nasr Jun 10 '14 at 19:12
  • 2
    The example with `count()` is just to show that it is a `Countable` – Ocramius Jun 11 '14 at 21:04
  • 3
    I don't see the utility of that class Paginator, is not adding anything, I could get the same functionality with default object result – fpilee Apr 06 '17 at 23:09
  • 6
    Not if joins are involved: that is where the paginator provides reliable results. – Ocramius Apr 07 '17 at 14:29
-1

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>';
        }   
    }
  • Welcome to Stackoverflow and thanks for taking the time to answer! An answer should not only contain a block of code however, please add some more descriptive text describing your solution as well. – David Mulder Mar 16 '14 at 00:21