Skip to content
Merged
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
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
17 changes: 16 additions & 1 deletion src/Datagrid/ProxyQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator;

/**
* This class try to unify the query usage with Doctrine.
Expand Down Expand Up @@ -205,7 +206,21 @@ public function execute(array $params = [], $hydrationMode = null)
$query->setHint($name, $value);
}

return $query->execute($params, $hydrationMode);
// NEXT_MAJOR: Remove this.
if (\func_num_args() > 0) {
return $query->execute($params, $hydrationMode);
}

$identifierFieldNames = $this
->getQueryBuilder()
->getEntityManager()
->getMetadataFactory()
->getMetadataFor(current($this->getQueryBuilder()->getRootEntities()))
->getIdentifierFieldNames();

// Paginator with fetchJoinCollection doesn't work with composite primary keys
// https://github.com/doctrine/orm/issues/2910
return new Paginator($query, 1 === \count($identifierFieldNames));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/App/Admin/AuthorAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* @phpstan-extends AbstractAdmin<\Sonata\DoctrineORMAdminBundle\Tests\App\Entity\Author>
*/
final class AuthorAdmin extends AbstractAdmin
class AuthorAdmin extends AbstractAdmin
{
protected function configureListFields(ListMapper $list): void
{
Expand Down
20 changes: 20 additions & 0 deletions tests/App/Admin/AuthorWithSimplePagerAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineORMAdminBundle\Tests\App\Admin;

final class AuthorWithSimplePagerAdmin extends AuthorAdmin
{
protected $baseRoutePattern = 'author-with-simple-pager';
protected $baseRouteName = 'author_with_simple_pager';
}
16 changes: 16 additions & 0 deletions tests/App/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
* file that was distributed with this source code.
*/

use Sonata\AdminBundle\Datagrid\Pager;
use Sonata\DoctrineORMAdminBundle\Tests\App\Admin\AuthorAdmin;
use Sonata\DoctrineORMAdminBundle\Tests\App\Admin\AuthorWithSimplePagerAdmin;
use Sonata\DoctrineORMAdminBundle\Tests\App\Admin\BookAdmin;
use Sonata\DoctrineORMAdminBundle\Tests\App\Admin\CarAdmin;
use Sonata\DoctrineORMAdminBundle\Tests\App\Admin\CategoryAdmin;
Expand Down Expand Up @@ -58,6 +60,20 @@
->tag('sonata.admin', [
'manager_type' => 'orm',
'label' => 'Author',
'default' => true,
])
->args([
'',
Author::class,
null,
])
->call('setTemplate', ['outer_list_rows_list', 'author/list_outer_list_rows_list.html.twig'])

->set(AuthorWithSimplePagerAdmin::class)
->tag('sonata.admin', [
'manager_type' => 'orm',
'label' => 'Author with Simple Pager',
'pager_type' => Pager::TYPE_SIMPLE,
])
->args([
'',
Expand Down
6 changes: 3 additions & 3 deletions 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 Expand Up @@ -142,7 +142,7 @@ public function testExecuteWithOrderBy(): void
['id' => 2],
['id' => 3],
],
$query->execute()
iterator_to_array($query->execute())
);

$query2 = new ProxyQuery(
Expand All @@ -156,7 +156,7 @@ public function testExecuteWithOrderBy(): void
['id' => 1],
['id' => 3],
],
$query2->execute()
iterator_to_array($query2->execute())
);
}
}
30 changes: 30 additions & 0 deletions tests/Functional/SimplePagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineORMAdminBundle\Tests\Functional;

use Symfony\Component\HttpFoundation\Request;

final class SimplePagerTest extends BaseFunctionalTestCase
{
public function testSimplePagerSameResultsAsPager(): void
{
$crawlerWithPager = $this->client->request(Request::METHOD_GET, '/admin/tests/app/author/list');

$numberOfAuthors = $crawlerWithPager->filter('.js-author-item')->count();

$crawlerWithSimplePager = $this->client->request(Request::METHOD_GET, '/admin/author-with-simple-pager/list');

$this->assertSame($numberOfAuthors, $crawlerWithSimplePager->filter('.js-author-item')->count());
}
}