Skip to content

Instantly share code, notes, and snippets.

@atillay
Last active July 24, 2024 17:19
Show Gist options
  • Select an option

  • Save atillay/f309aff35a6e4dd19a16c92caa8669ab to your computer and use it in GitHub Desktop.

Select an option

Save atillay/f309aff35a6e4dd19a16c92caa8669ab to your computer and use it in GitHub Desktop.

Revisions

  1. Alexandre Tillay revised this gist May 11, 2018. No changes.
  2. Alexandre Tillay revised this gist May 11, 2018. 3 changed files with 54 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions DemoController.php
    Original 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.
    30 changes: 30 additions & 0 deletions _pagination.html.twig
    Original 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">&laquo;</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">&raquo;</span>
    <span class="sr-only">Next</span>
    </a>
    </li>
    {% endif %}
    </ul>
    </nav>
    {% endif %}
  3. Alexandre Tillay revised this gist May 11, 2018. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions Pagination.php
    Original 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 Pagination
    class PaginationService
    {
    /**
    * @param QueryBuilder|Query $query
    * @param Request $request
    * @param int $limit
    * @param int $currentPage
    * @return Paginator
    */
    public function paginate($query, int $limit, int $currentPage): Paginator
    public function paginate($query, Request $request, int $limit): Paginator
    {
    $currentPage = $request->query->getInt('p') ?: 1;
    $paginator = new Paginator($query);
    $paginator
    ->getQuery()
  4. Alexandre Tillay revised this gist May 11, 2018. 1 changed file with 0 additions and 9 deletions.
    9 changes: 0 additions & 9 deletions Pagination.php
    Original file line number Diff line number Diff line change
    @@ -8,15 +8,6 @@

    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
  5. Alexandre Tillay revised this gist May 11, 2018. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion Pagination.php
    Original 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;
    use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

    class Pagination
    {
  6. Alexandre Tillay revised this gist May 11, 2018. 1 changed file with 2 additions and 6 deletions.
    8 changes: 2 additions & 6 deletions Pagination.php
    Original 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);

    if ($this->hasNoResult($paginator)) {
    throw new NotFoundHttpException();
    }

    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 hasNoResult(Paginator $paginator): bool
    public function currentPageHasNoResult(Paginator $paginator): bool
    {
    return !$paginator->getIterator()->count();
    }
  7. Alexandre Tillay revised this gist May 11, 2018. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions Pagination.php
    Original 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
  8. Alexandre Tillay revised this gist May 11, 2018. No changes.
  9. Alexandre Tillay renamed this gist May 11, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. Alexandre Tillay revised this gist May 11, 2018. No changes.
  11. Alexandre Tillay created this gist May 11, 2018.
    59 changes: 59 additions & 0 deletions Pagination
    Original 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();
    }
    }