Skip to content
Open
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
17 changes: 16 additions & 1 deletion src/Tools/Pagination/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,22 @@ private function appendTreeWalker(Query $query, string $walkerClass): void
*/
private function getCountQuery(): Query
{
$countQuery = $this->cloneQuery($this->query);
/*
As opposed to using self::cloneQuery, the following code does not transfer
a potentially existing result set mapping (either set directly by the user,
or taken from the parser result from a previous invocation of Query::parse())
to the new query object. This is fine, since we are going to completely change the
select clause anyway, so a previously existing result set mapping (RSM) is probably wrong anyway.
In the case of using output walkers, we are even creating a new RSM down below.
In the case of using a tree walker, we want to have a new RSM created by the parser.
*/
$countQuery = new Query($this->query->getEntityManager());
$countQuery->setDQL($this->query->getDQL());
$countQuery->setParameters(clone $this->query->getParameters());
$countQuery->setCacheable(false);
foreach ($this->query->getHints() as $name => $value) {
$countQuery->setHint($name, $value);
}

if (! $countQuery->hasHint(CountWalker::HINT_DISTINCT)) {
$countQuery->setHint(CountWalker::HINT_DISTINCT, true);
Expand Down
46 changes: 46 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH12183Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

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

final class GH12183Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
$this->useModelSet('cms');

parent::setUp();

$article = new CmsArticle();

$article->topic = 'Loomings';
$article->text = 'Call me Ishmael.';

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

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

// Paginator::count is right when the query has not yet been executed
$paginator = new Paginator($query);
$paginator->setUseOutputWalkers(false);
self::assertSame(1, $paginator->count());

// Execute the query
$result = $query->getResult();
self::assertCount(1, $result);

$paginator = new Paginator($query);
$paginator->setUseOutputWalkers(false);
self::assertSame(1, $paginator->count());
}
}