Skip to content

Commit

Permalink
Merge pull request #7863 from Seb33300/skip-limit-subquery
Browse files Browse the repository at this point in the history
Paginator: Skip limit subquery if not required
  • Loading branch information
lcobucci committed Nov 16, 2019
2 parents 9162f35 + 6347190 commit 8c47dcb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Tools/Pagination/Paginator.php
Expand Up @@ -139,7 +139,7 @@ public function getIterator()
$offset = $this->query->getFirstResult();
$length = $this->query->getMaxResults();

if ($this->fetchJoinCollection) {
if ($this->fetchJoinCollection && $length !== null) {
$subQuery = $this->cloneQuery($this->query);

if ($this->useOutputWalker($subQuery)) {
Expand Down
1 change: 1 addition & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1685Test.php
Expand Up @@ -29,6 +29,7 @@ protected function setUp()

$dql = "SELECT ad FROM Doctrine\Tests\Models\DDC117\DDC117ArticleDetails ad";
$query = $this->_em->createQuery($dql);
$query->setMaxResults(1);

$this->paginator = new Paginator($query);
}
Expand Down
63 changes: 63 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH7829Test.php
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Doctrine\Tests\Models\CMS\CmsArticle;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
* @group GH7829
*/
final class GH7829Test extends OrmFunctionalTestCase
{
/** @var DebugStack */
private $logger;

protected function setUp()
{
$this->useModelSet('cms');
parent::setUp();

$article = new CmsArticle();

$article->topic = 'Skip Limit Subquery';
$article->text = 'Skip Limit Subquery if not required.';

$this->_em->persist($article);
$this->_em->flush();
$this->_em->clear();

$this->_em->getConnection()->getConfiguration()->setSQLLogger($this->logger = new DebugStack());
}

public function testPaginatorWithLimitSubquery() : void
{
$query = $this->_em->createQuery('SELECT a FROM Doctrine\Tests\Models\CMS\CmsArticle a');
$query->setMaxResults(1);

$paginator = new Paginator($query, true);
$paginator->setUseOutputWalkers(false);

$paginator->count();
$paginator->getIterator();

$this->assertCount(3, $this->logger->queries);
}

public function testPaginatorWithLimitSubquerySkipped() : void
{
$query = $this->_em->createQuery('SELECT a FROM Doctrine\Tests\Models\CMS\CmsArticle a');

$paginator = new Paginator($query, true);
$paginator->setUseOutputWalkers(false);

$paginator->count();
$paginator->getIterator();

$this->assertCount(2, $this->logger->queries);
}
}
2 changes: 2 additions & 0 deletions tests/Doctrine/Tests/ORM/Tools/Pagination/PaginatorTest.php
Expand Up @@ -61,6 +61,7 @@ public function testExtraParametersAreStrippedWhenWalkerRemovingOriginalSelectEl
WHERE u.id = :paramInWhere'
);
$query->setParameters(['paramInWhere' => $paramInWhere, 'paramInSubSelect' => $paramInSubSelect]);
$query->setMaxResults(1);
$paginator = (new Paginator($query, true))->setUseOutputWalkers(false);

$this->connection->expects($this->exactly(3))->method('executeQuery');
Expand Down Expand Up @@ -113,6 +114,7 @@ private function createPaginatorWithExtraParametersWithoutOutputWalkers(array $w
$query = new Query($this->em);
$query->setDQL('SELECT u FROM Doctrine\\Tests\\Models\\CMS\\CmsUser u');
$query->setParameters(['paramInWhere' => 1]);
$query->setMaxResults(1);

return (new Paginator($query, true))->setUseOutputWalkers(false);
}
Expand Down

0 comments on commit 8c47dcb

Please sign in to comment.