Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. <?php
  2.  
  3. use Doctrine\ORM\Query;
  4. use Doctrine\ORM\Tools\Pagination\Paginator;
  5.  
  6. // ------------------- //
  7.  
  8. public function paginate(Query $query, int $page = 1, int $limit = 10, bool $fetchJoinCollection = true)
  9. {
  10. // If we don't have a limit just return the result
  11. if ($page === 1 && $limit === 0) {
  12. return $query->getResult();
  13. }
  14.  
  15. $paginator = new Paginator(
  16. $query->setFirstResult(($page * $limit) - $limit) // Offset
  17. ->setMaxResults($limit), // Limit
  18. $fetchJoinCollection
  19. );
  20.  
  21. // Num results
  22. $count = $paginator->count();
  23.  
  24. return [
  25. 'result' => $paginator->getIterator()->getArrayCopy(),// `getArrayCopy()` for to be passed to `array_map()` directly
  26. 'meta' => [
  27. 'total' => (int)$count,
  28. 'page' => (int)$page,
  29. 'pages' => (int)ceil($count / $limit)
  30. ]
  31. ];
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement