From 4346a407760006d50e8b90cb05f3d38321809c56 Mon Sep 17 00:00:00 2001 From: Antoine Date: Wed, 15 May 2024 11:26:15 +0200 Subject: [PATCH] :bug: Fix label & method countResults to display good things with simple pager --- src/Datagrid/SimplePager.php | 27 +++++++++++++++---- .../Pager/simple_pager_results.html.twig | 9 +------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Datagrid/SimplePager.php b/src/Datagrid/SimplePager.php index 582da46d39..8074eb0c33 100644 --- a/src/Datagrid/SimplePager.php +++ b/src/Datagrid/SimplePager.php @@ -15,6 +15,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Sonata\AdminBundle\Util\TraversableToCollection; +use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery; /** * @author Lukas Kahwe Smith @@ -44,16 +45,14 @@ final class SimplePager extends Pager * If set to 3 the pager will generate links to the next three pages * etc. */ - public function __construct( - int $maxPerPage = 10, - private int $threshold = 1 - ) { + public function __construct(int $maxPerPage = 10, private int $threshold = 1, private int $total = 0) + { parent::__construct($maxPerPage); } public function countResults(): int { - return ($this->getPage() - 1) * $this->getMaxPerPage() + ($this->thresholdCount ?? 0); + return $this->total; } public function getCurrentPageResults(): iterable @@ -79,6 +78,23 @@ public function getCurrentPageResults(): iterable return $this->results; } + public function getTotalResults(): int + { + /** @var ProxyQuery $query */ + $query = $this->getQuery(); + + if (null === $query) { + throw new \LogicException('Uninitialized query.'); + } + + $query->select(sprintf('COUNT(%s.id)', $query->getRootAlias())); + $query->setFirstResult(0); + $query->setMaxResults(1); + + + return $query->getDoctrineQuery()->getSingleScalarResult(); + } + /** * @throws \LogicException the query is uninitialized */ @@ -103,6 +119,7 @@ public function init(): void $query->setMaxResults($maxOffset); $this->results = $this->getCurrentPageResults(); + $this->total = $this->getTotalResults(); $t = (int) ceil(($this->thresholdCount ?? 0) / $this->getMaxPerPage()) + $this->getPage() - 1; $this->setLastPage(max(1, $t)); diff --git a/src/Resources/views/Pager/simple_pager_results.html.twig b/src/Resources/views/Pager/simple_pager_results.html.twig index 3660ce5f70..0ca7f6f817 100644 --- a/src/Resources/views/Pager/simple_pager_results.html.twig +++ b/src/Resources/views/Pager/simple_pager_results.html.twig @@ -12,9 +12,6 @@ file that was distributed with this source code. {% extends '@SonataAdmin/Pager/base_results.html.twig' %} {% block num_results %} - {% if admin.datagrid.pager.lastPage != admin.datagrid.pager.page %} - {{ 'list_results_count_prefix'|trans({}, 'SonataAdminBundle') }} - {% endif %} {% trans with {'%count%': admin.datagrid.pager.countResults()} from 'SonataAdminBundle' %}list_results_count{% endtrans %}  -  {% endblock %} @@ -22,10 +19,6 @@ file that was distributed with this source code. {% block num_pages %} {{ admin.datagrid.pager.page }} / - {% if admin.datagrid.pager.lastPage != admin.datagrid.pager.page %} - ? - {% else %} - {{ admin.datagrid.pager.lastpage }} - {% endif %} + {{ admin.datagrid.pager.lastPage }}  -  {% endblock %}