From 45550026d42de438fd5f1631fb1337cfb3afd7ce Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 25 Apr 2021 14:04:04 +0200 Subject: [PATCH] Use `execute` method directly in the pager --- src/Datagrid/Pager.php | 44 ++----------------------------- src/Datagrid/ProxyQuery.php | 4 +-- tests/Datagrid/ProxyQueryTest.php | 2 +- 3 files changed, 4 insertions(+), 46 deletions(-) diff --git a/src/Datagrid/Pager.php b/src/Datagrid/Pager.php index 60f835e6b..0f610ce8c 100644 --- a/src/Datagrid/Pager.php +++ b/src/Datagrid/Pager.php @@ -14,7 +14,6 @@ namespace Sonata\DoctrineORMAdminBundle\Datagrid; use Doctrine\ORM\Query; -use Doctrine\ORM\Tools\Pagination\Paginator; use Sonata\AdminBundle\Datagrid\Pager as BasePager; /** @@ -70,31 +69,7 @@ public function computeNbResult() public function getCurrentPageResults(): iterable { - $query = $this->getQuery(); - if (!$query instanceof ProxyQueryInterface) { - throw new \TypeError(sprintf( - 'The pager query MUST implement %s.', - ProxyQueryInterface::class, - )); - } - - $identifierFieldNames = $query - ->getQueryBuilder() - ->getEntityManager() - ->getMetadataFactory() - ->getMetadataFor(current($query->getQueryBuilder()->getRootEntities())) - ->getIdentifierFieldNames(); - - // NEXT_MAJOR: Remove the check and the else part. - if (method_exists($query, 'getDoctrineQuery')) { - // Paginator with fetchJoinCollection doesn't work with composite primary keys - // https://github.com/doctrine/orm/issues/2910 - $paginator = new Paginator($query->getDoctrineQuery(), 1 === \count($identifierFieldNames)); - } else { - $paginator = new Paginator($query->getQueryBuilder(), 1 === \count($identifierFieldNames)); - } - - return $paginator->getIterator(); + return $this->getQuery()->execute(); } /** @@ -207,27 +182,12 @@ private function computeResultsCount(): int // NEXT_MAJOR: remove the clone. $query = clone $this->getQuery(); - if (!$query instanceof ProxyQueryInterface) { - throw new \TypeError(sprintf( - 'The pager query MUST implement %s, %s provided.', - ProxyQueryInterface::class, - \is_object($query) ? sprintf('instance of %s', \get_class($query)) : \gettype($query) - )); - } - // NEXT_MAJOR: Remove this code. if (\count($this->getParameters('sonata_deprecation_mute')) > 0) { $query->setParameters($this->getParameters('sonata_deprecation_mute')); } - // NEXT_MAJOR: Remove the check and the else part. - if (method_exists($query, 'getDoctrineQuery')) { - $paginator = new Paginator($query->getDoctrineQuery()); - } else { - $paginator = new Paginator($query->getQueryBuilder()); - } - - return \count($paginator); + return \count($this->getQuery()->execute()); } private function setResultsCount(int $count): void diff --git a/src/Datagrid/ProxyQuery.php b/src/Datagrid/ProxyQuery.php index 6353eb06b..3732eb04d 100644 --- a/src/Datagrid/ProxyQuery.php +++ b/src/Datagrid/ProxyQuery.php @@ -220,9 +220,7 @@ public function execute(array $params = [], $hydrationMode = null) // Paginator with fetchJoinCollection doesn't work with composite primary keys // https://github.com/doctrine/orm/issues/2910 - $paginator = new Paginator($query, 1 === \count($identifierFieldNames)); - - return $paginator->getIterator(); + return new Paginator($query, 1 === \count($identifierFieldNames)); } /** diff --git a/tests/Datagrid/ProxyQueryTest.php b/tests/Datagrid/ProxyQueryTest.php index 8918d4235..ad2f09329 100644 --- a/tests/Datagrid/ProxyQueryTest.php +++ b/tests/Datagrid/ProxyQueryTest.php @@ -83,7 +83,7 @@ public function testSetHint(): void ); $pq->setHint('hint', 'value'); - $result = $pq->execute(); + $result = iterator_to_array($pq->execute()); $this->assertSame(2, $result[0]['id']); }