Skip to content

Commit 11c1717

Browse files
committed
feature #991 Switch to Doctrine's Pager (javiereguiluz)
This PR was squashed before being merged into the master branch (closes #991). Discussion ---------- Switch to Doctrine's Pager Pagerfanta library could be soon be unmaintained: whiteoctober/Pagerfanta#278 Let's switch to Doctrine Pager. Commits ------- 65872cd Switch to Doctrine's Pager
2 parents 3c6a6f2 + 65872cd commit 11c1717

File tree

7 files changed

+61
-156
lines changed

7 files changed

+61
-156
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
"symfony/yaml": "*",
3232
"tgalopin/html-sanitizer-bundle": "^1.1",
3333
"twig/extensions": "^1.5",
34-
"twig/twig": "^2.6",
35-
"white-october/pagerfanta-bundle": "^1.1"
34+
"twig/twig": "^2.6"
3635
},
3736
"require-dev": {
3837
"dama/doctrine-test-bundle": "^5.0",

composer.lock

Lines changed: 1 addition & 128 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/bundles.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
1010
Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true],
1111
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
12-
WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle::class => ['all' => true],
1312
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true],
1413
Symfony\Bundle\WebServerBundle\WebServerBundle::class => ['dev' => true],
1514
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],

src/Controller/BlogController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public function index(Request $request, int $page, string $_format, PostReposito
5757
// Every template name also has two extensions that specify the format and
5858
// engine for that template.
5959
// See https://symfony.com/doc/current/templating.html#template-suffix
60-
return $this->render('blog/index.'.$_format.'.twig', ['posts' => $latestPosts]);
60+
return $this->render('blog/index.'.$_format.'.twig', [
61+
'paginator' => $latestPosts,
62+
]);
6163
}
6264

6365
/**

src/Repository/PostRepository.php

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
use App\Entity\Tag;
1616
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
1717
use Doctrine\Common\Persistence\ManagerRegistry;
18-
use Doctrine\ORM\Query;
19-
use Pagerfanta\Adapter\DoctrineORMAdapter;
20-
use Pagerfanta\Pagerfanta;
18+
use Doctrine\ORM\QueryBuilder;
19+
use Doctrine\ORM\Tools\Pagination\Paginator;
2120

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

39-
public function findLatest(int $page = 1, Tag $tag = null): Pagerfanta
38+
public function findLatest(int $page = 1, Tag $tag = null): array
4039
{
4140
$qb = $this->createQueryBuilder('p')
4241
->addSelect('a', 't')
4342
->innerJoin('p.author', 'a')
4443
->leftJoin('p.tags', 't')
4544
->where('p.publishedAt <= :now')
4645
->orderBy('p.publishedAt', 'DESC')
47-
->setParameter('now', new \DateTime());
46+
->setParameter('now', new \DateTime())
47+
;
4848

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

54-
return $this->createPaginator($qb->getQuery(), $page);
55-
}
56-
57-
private function createPaginator(Query $query, int $page): Pagerfanta
58-
{
59-
$paginator = new Pagerfanta(new DoctrineORMAdapter($query));
60-
$paginator->setMaxPerPage(Post::NUM_ITEMS);
61-
$paginator->setCurrentPage($page);
62-
63-
return $paginator;
54+
return $this->createPaginator($qb, $page);
6455
}
6556

6657
/**
@@ -110,4 +101,31 @@ private function extractSearchTerms(string $searchQuery): array
110101
return 2 <= mb_strlen($term);
111102
});
112103
}
104+
105+
private function createPaginator(QueryBuilder $queryBuilder, int $currentPage, int $pageSize = Post::NUM_ITEMS)
106+
{
107+
$currentPage = $currentPage < 1 ? 1 : $currentPage;
108+
$firstResult = ($currentPage - 1) * $pageSize;
109+
110+
$query = $queryBuilder
111+
->setFirstResult($firstResult)
112+
->setMaxResults($pageSize)
113+
->getQuery();
114+
115+
$paginator = new Paginator($query);
116+
$numResults = $paginator->count();
117+
$hasPreviousPage = $currentPage > 1;
118+
$hasNextPage = ($currentPage * $pageSize) < $numResults;
119+
120+
return [
121+
'results' => $paginator->getIterator(),
122+
'currentPage' => $currentPage,
123+
'hasPreviousPage' => $hasPreviousPage,
124+
'hasNextPage' => $hasNextPage,
125+
'previousPage' => $hasPreviousPage ? $currentPage - 1 : null,
126+
'nextPage' => $hasNextPage ? $currentPage + 1 : null,
127+
'numPages' => (int) ceil($numResults / $pageSize),
128+
'haveToPaginate' => $numResults > $pageSize,
129+
];
130+
}
113131
}

symfony.lock

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,6 @@
128128
"ocramius/proxy-manager": {
129129
"version": "2.1.x-dev"
130130
},
131-
"pagerfanta/pagerfanta": {
132-
"version": "v2.0.1"
133-
},
134131
"paragonie/random_compat": {
135132
"version": "v9.99.99.x-dev"
136133
},
@@ -473,9 +470,6 @@
473470
"twig/twig": {
474471
"version": "2.x-dev"
475472
},
476-
"white-october/pagerfanta-bundle": {
477-
"version": "v1.2.2"
478-
},
479473
"zendframework/zend-code": {
480474
"version": "3.3-dev"
481475
}

templates/blog/index.html.twig

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{% block body_id 'blog_index' %}
44

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

27-
{% if posts.haveToPaginate %}
27+
{% if paginator.haveToPaginate %}
2828
<div class="navigation text-center">
29-
{{ pagerfanta(posts, 'twitter_bootstrap3_translated', {routeName: 'blog_index_paginated', routeParams: app.request.query.all}) }}
29+
<ul class="pagination">
30+
{% if paginator.hasPreviousPage %}
31+
<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>
32+
{% else %}
33+
<li class="prev disabled"><span><i class="fa fw fa-arrow-left"></i> Previous</span></li>
34+
{% endif %}
35+
36+
{% for i in 1..paginator.numPages %}
37+
{% if i == paginator.currentPage %}
38+
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
39+
{% else %}
40+
<li><a href="{{ path('blog_index_paginated', {page: i}) }}">{{ i }}</a></li>
41+
{% endif %}
42+
{% endfor %}
43+
44+
{% if paginator.hasNextPage %}
45+
<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>
46+
{% else %}
47+
<li class="next disabled"><span>Next <i class="fa fw fa-arrow-right"></i></span></li>
48+
{% endif %}
49+
</ul>
3050
</div>
3151
{% endif %}
3252
{% endblock %}

0 commit comments

Comments
 (0)