Last active
July 24, 2024 17:19
-
-
Save atillay/f309aff35a6e4dd19a16c92caa8669ab to your computer and use it in GitHub Desktop.
Revisions
-
Alexandre Tillay revised this gist
May 11, 2018 . No changes.There are no files selected for viewing
-
Alexandre Tillay revised this gist
May 11, 2018 . 3 changed files with 54 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ <?php namespace App\Controller; use App\Entity\Demo; use App\Service\PaginationService; use Symfony\Component\HttpFoundation\Request; class DemoController extends AdminController { const ITEMS_PER_PAGE = 50; public function index(Request $request, PaginationService $pagination) { $query = $this->em->getRepository(Demo::class)->getQuery(); $results = $pagination->paginate($query, $request, self::ITEMS_PER_PAGE); return $this->render('admin/school/index.html.twig', [ 'schools' => $schools, 'lastPage' => $pagination->lastPage($results) ]); } } File renamed without changes.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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ {% set _currentPage = app.request.query.get('p') ?: 1 %} {% set _currentRoute = app.request.attributes.get('_route') %} {% set _currentParams = app.request.query.all %} {% if lastPage > 1 %} <nav> <ul class="pagination"> {% if _currentPage > 1 %} <li class="page-item"> <a class="page-link" href="{{ path(_currentRoute, _currentParams|merge({p: _currentPage - 1})) }}" aria-label="Previous"> <span aria-hidden="true">«</span> <span class="sr-only">Previous</span> </a> </li> {% endif %} {% for i in 1..lastPage %} <li class="page-item {% if i == _currentPage %}active{% endif %}"> <a class="page-link" href="{{ path(_currentRoute, _currentParams|merge({p: i})) }}">{{ i }}</a> </li> {% endfor %} {% if _currentPage < lastPage %} <li class="page-item"> <a class="page-link" href="{{ path(_currentRoute, _currentParams|merge({p: _currentPage + 1})) }}" aria-label="Next"> <span aria-hidden="true">»</span> <span class="sr-only">Next</span> </a> </li> {% endif %} </ul> </nav> {% endif %} -
Alexandre Tillay revised this gist
May 11, 2018 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,17 +5,19 @@ use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\Tools\Pagination\Paginator; use Symfony\Component\HttpFoundation\Request; class PaginationService { /** * @param QueryBuilder|Query $query * @param Request $request * @param int $limit * @return Paginator */ public function paginate($query, Request $request, int $limit): Paginator { $currentPage = $request->query->getInt('p') ?: 1; $paginator = new Paginator($query); $paginator ->getQuery() -
Alexandre Tillay revised this gist
May 11, 2018 . 1 changed file with 0 additions and 9 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -8,15 +8,6 @@ class Pagination { /** * @param QueryBuilder|Query $query * @param int $limit -
Alexandre Tillay revised this gist
May 11, 2018 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,6 @@ use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\Tools\Pagination\Paginator; class Pagination { -
Alexandre Tillay revised this gist
May 11, 2018 . 1 changed file with 2 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -32,10 +32,6 @@ public function paginate($query, int $limit, int $currentPage): Paginator ->setFirstResult($limit * ($currentPage - 1)) ->setMaxResults($limit); return $paginator; } @@ -47,7 +43,7 @@ public function lastPage(Paginator $paginator): int { return ceil($paginator->count() / $paginator->getQuery()->getMaxResults()); } /** * @param Paginator $paginator * @return int @@ -61,7 +57,7 @@ public function total(Paginator $paginator): int * @param Paginator $paginator * @return bool */ public function currentPageHasNoResult(Paginator $paginator): bool { return !$paginator->getIterator()->count(); } -
Alexandre Tillay revised this gist
May 11, 2018 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -47,6 +47,15 @@ public function lastPage(Paginator $paginator): int { return ceil($paginator->count() / $paginator->getQuery()->getMaxResults()); } /** * @param Paginator $paginator * @return int */ public function total(Paginator $paginator): int { return $paginator->count(); } /** * @param Paginator $paginator -
Alexandre Tillay revised this gist
May 11, 2018 . No changes.There are no files selected for viewing
-
Alexandre Tillay renamed this gist
May 11, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Alexandre Tillay revised this gist
May 11, 2018 . No changes.There are no files selected for viewing
-
Alexandre Tillay created this gist
May 11, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,59 @@ <?php namespace App\Service; use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\Tools\Pagination\Paginator; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class Pagination { /** USAGE ** $limit = 10; $currentPage = $request->query->getInt('p') ?: 1; $myQuery = $em->getRepository(MyRepo::class)->myQuery(); $results = $pagination->paginate($myQuery, $limit, $currentPage); $lastPage = $pagination->lastPage($results); $total = $results->count(); ****/ /** * @param QueryBuilder|Query $query * @param int $limit * @param int $currentPage * @return Paginator */ public function paginate($query, int $limit, int $currentPage): Paginator { $paginator = new Paginator($query); $paginator ->getQuery() ->setFirstResult($limit * ($currentPage - 1)) ->setMaxResults($limit); if ($this->hasNoResult($paginator)) { throw new NotFoundHttpException(); } return $paginator; } /** * @param Paginator $paginator * @return int */ public function lastPage(Paginator $paginator): int { return ceil($paginator->count() / $paginator->getQuery()->getMaxResults()); } /** * @param Paginator $paginator * @return bool */ public function hasNoResult(Paginator $paginator): bool { return !$paginator->getIterator()->count(); } }