Created
March 6, 2012 12:37
-
-
Save peterjmit/1986048 to your computer and use it in GitHub Desktop.
Helper function for paginating with native doctrine 2.2 class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function paginate(Doctrine\ORM\Query $query, $page, $limit, $fetchJoinCollection = true) | |
{ | |
// If we don't have a page just return the result | |
if( ! $page && ! $limit) | |
{ | |
return $query->getResult(); | |
} | |
$offset = ($page * $limit) - $limit; | |
$query->setFirstResult($offset); | |
$query->setMaxResults($limit); | |
$paginator = new Doctrine\ORM\Tools\Pagination\Paginator($query, $fetchJoinCollection); | |
// Num results | |
$count = $paginator->count(); | |
$pagination_info = array( | |
'total' => (int) $count, | |
'current_page' => (int) $page, | |
'total_pages' => (int) ceil($count / $limit) | |
); | |
return array( | |
'result' => $paginator, // could just return $paginator->getIterator() | |
'meta' => $pagination_info | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment