Skip to content

Switch to Doctrine's Pager #991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
"symfony/yaml": "*",
"tgalopin/html-sanitizer-bundle": "^1.1",
"twig/extensions": "^1.5",
"twig/twig": "^2.6",
"white-october/pagerfanta-bundle": "^1.1"
"twig/twig": "^2.6"
},
"require-dev": {
"dama/doctrine-test-bundle": "^5.0",
Expand Down
129 changes: 1 addition & 128 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle::class => ['all' => true],
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\WebServerBundle\WebServerBundle::class => ['dev' => true],
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
Expand Down
4 changes: 3 additions & 1 deletion src/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public function index(Request $request, int $page, string $_format, PostReposito
// Every template name also has two extensions that specify the format and
// engine for that template.
// See https://symfony.com/doc/current/templating.html#template-suffix
return $this->render('blog/index.'.$_format.'.twig', ['posts' => $latestPosts]);
return $this->render('blog/index.'.$_format.'.twig', [
'paginator' => $latestPosts,
]);
}

/**
Expand Down
48 changes: 33 additions & 15 deletions src/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
use App\Entity\Tag;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Query;
use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Pagerfanta;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator;

/**
* This custom Doctrine repository contains some methods which are useful when
Expand All @@ -36,31 +35,23 @@ public function __construct(ManagerRegistry $registry)
parent::__construct($registry, Post::class);
}

public function findLatest(int $page = 1, Tag $tag = null): Pagerfanta
public function findLatest(int $page = 1, Tag $tag = null): array
{
$qb = $this->createQueryBuilder('p')
->addSelect('a', 't')
->innerJoin('p.author', 'a')
->leftJoin('p.tags', 't')
->where('p.publishedAt <= :now')
->orderBy('p.publishedAt', 'DESC')
->setParameter('now', new \DateTime());
->setParameter('now', new \DateTime())
;

if (null !== $tag) {
$qb->andWhere(':tag MEMBER OF p.tags')
->setParameter('tag', $tag);
}

return $this->createPaginator($qb->getQuery(), $page);
}

private function createPaginator(Query $query, int $page): Pagerfanta
{
$paginator = new Pagerfanta(new DoctrineORMAdapter($query));
$paginator->setMaxPerPage(Post::NUM_ITEMS);
$paginator->setCurrentPage($page);

return $paginator;
return $this->createPaginator($qb, $page);
}

/**
Expand Down Expand Up @@ -110,4 +101,31 @@ private function extractSearchTerms(string $searchQuery): array
return 2 <= mb_strlen($term);
});
}

private function createPaginator(QueryBuilder $queryBuilder, int $currentPage, int $pageSize = Post::NUM_ITEMS)
{
$currentPage = $currentPage < 1 ? 1 : $currentPage;
$firstResult = ($currentPage - 1) * $pageSize;

$query = $queryBuilder
->setFirstResult($firstResult)
->setMaxResults($pageSize)
->getQuery();

$paginator = new Paginator($query);
$numResults = $paginator->count();
$hasPreviousPage = $currentPage > 1;
$hasNextPage = ($currentPage * $pageSize) < $numResults;

return [
'results' => $paginator->getIterator(),
'currentPage' => $currentPage,
'hasPreviousPage' => $hasPreviousPage,
'hasNextPage' => $hasNextPage,
'previousPage' => $hasPreviousPage ? $currentPage - 1 : null,
'nextPage' => $hasNextPage ? $currentPage + 1 : null,
'numPages' => (int) ceil($numResults / $pageSize),
'haveToPaginate' => $numResults > $pageSize,
];
}
}
6 changes: 0 additions & 6 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@
"ocramius/proxy-manager": {
"version": "2.1.x-dev"
},
"pagerfanta/pagerfanta": {
"version": "v2.0.1"
},
"paragonie/random_compat": {
"version": "v9.99.99.x-dev"
},
Expand Down Expand Up @@ -473,9 +470,6 @@
"twig/twig": {
"version": "2.x-dev"
},
"white-october/pagerfanta-bundle": {
"version": "v1.2.2"
},
"zendframework/zend-code": {
"version": "3.3-dev"
}
Expand Down
26 changes: 23 additions & 3 deletions templates/blog/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block body_id 'blog_index' %}

{% block main %}
{% for post in posts %}
{% for post in paginator.results %}
<article class="post">
<h2>
<a href="{{ path('blog_post', {slug: post.slug}) }}">
Expand All @@ -24,9 +24,29 @@
<div class="well">{{ 'post.no_posts_found'|trans }}</div>
{% endfor %}

{% if posts.haveToPaginate %}
{% if paginator.haveToPaginate %}
<div class="navigation text-center">
{{ pagerfanta(posts, 'twitter_bootstrap3_translated', {routeName: 'blog_index_paginated', routeParams: app.request.query.all}) }}
<ul class="pagination">
{% if paginator.hasPreviousPage %}
<li class="prev"><a href="{{ path('blog_index_paginated', {page: paginator.previousPage}) }}" rel="previous"><i class="fa fw fa-long-arrow-left"></i> Previous</a></li>
{% else %}
<li class="prev disabled"><span><i class="fa fw fa-arrow-left"></i> Previous</span></li>
{% endif %}

{% for i in 1..paginator.numPages %}
{% if i == paginator.currentPage %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="{{ path('blog_index_paginated', {page: i}) }}">{{ i }}</a></li>
{% endif %}
{% endfor %}

{% if paginator.hasNextPage %}
<li class="prev"><a href="{{ path('blog_index_paginated', {page: paginator.nextPage}) }}" rel="next">Next <i class="fa fw fa-arrow-right"></i></a></li>
{% else %}
<li class="next disabled"><span>Next <i class="fa fw fa-arrow-right"></i></span></li>
{% endif %}
</ul>
</div>
{% endif %}
{% endblock %}
Expand Down