Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paginator: Skip limit subquery if not required #7863

Merged
merged 1 commit into from Nov 16, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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