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
14 changes: 11 additions & 3 deletions docs/en/cookbook/dql-custom-walkers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,16 @@ The ``Paginate::count(Query $query)`` looks like:
{
static public function count(Query $query)
{
/** @var Query $countQuery */
$countQuery = clone $query;
/*
To avoid changing the $query passed into the method and to make sure a possibly existing
ResultSetMapping is discarded, we create a new query object any copy relevant data over.
*/
$countQuery = new Query($query->getEntityManager());
$countQuery->setDQL($query->getDQL());
$countQuery->setParameters(clone $query->getParameters());
foreach ($query->getHints() as $name => $value) {
$countQuery->setHint($name, $value);
}
$countQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('DoctrineExtensions\Paginate\CountSqlWalker'));
$countQuery->setFirstResult(null)->setMaxResults(null);
Expand All @@ -111,7 +119,7 @@ The ``Paginate::count(Query $query)`` looks like:
}
}
It clones the query, resets the limit clause first and max results
This resets the limit clause first and max results
and registers the ``CountSqlWalker`` custom tree walker which
will modify the AST to execute a count query. The walkers
implementation is:
Expand Down
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, 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());
}
}