Skip to content

Commit

Permalink
Use execute method directly in the pager
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed May 2, 2021
1 parent ae31060 commit 4555002
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 46 deletions.
44 changes: 2 additions & 42 deletions src/Datagrid/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/Datagrid/ProxyQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Datagrid/ProxyQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand Down

0 comments on commit 4555002

Please sign in to comment.