Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use Doctrine\ORM\Query;
- use Doctrine\ORM\Tools\Pagination\Paginator;
- // ------------------- //
- public function paginate(Query $query, int $page = 1, int $limit = 10, bool $fetchJoinCollection = true)
- {
- // If we don't have a limit just return the result
- if ($page === 1 && $limit === 0) {
- return $query->getResult();
- }
- $paginator = new Paginator(
- $query->setFirstResult(($page * $limit) - $limit) // Offset
- ->setMaxResults($limit), // Limit
- $fetchJoinCollection
- );
- // Num results
- $count = $paginator->count();
- return [
- 'result' => $paginator->getIterator()->getArrayCopy(),// `getArrayCopy()` for to be passed to `array_map()` directly
- 'meta' => [
- 'total' => (int)$count,
- 'page' => (int)$page,
- 'pages' => (int)ceil($count / $limit)
- ]
- ];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement