diff --git a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php old mode 100644 new mode 100755 index 115eb590e9e..80438e03c99 --- a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php +++ b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php @@ -134,7 +134,7 @@ public function count() $countQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\CountOutputWalker'); $countQuery->setResultSetMapping($rsm); } else { - $countQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\CountWalker')); + $this->appendTreeWalker($countQuery, 'Doctrine\ORM\Tools\Pagination\CountWalker'); } $countQuery->setFirstResult(null)->setMaxResults(null); @@ -165,7 +165,7 @@ public function getIterator() if ($this->useOutputWalker($subQuery)) { $subQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker'); } else { - $subQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\LimitSubqueryWalker')); + $this->appendTreeWalker($subQuery, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryWalker'); } $subQuery->setFirstResult($offset)->setMaxResults($length); @@ -231,4 +231,22 @@ private function useOutputWalker(Query $query) return $this->useOutputWalkers; } + + /** + * Appends a custom tree walker to the tree walkers hint. + * + * @param Query $query + * @param string $walkerClass + */ + private function appendTreeWalker(Query $query, $walkerClass) + { + $hints = $query->getHint(Query::HINT_CUSTOM_TREE_WALKERS); + + if ($hints === false) { + $hints = array(); + } + + $hints[] = $walkerClass; + $query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, $hints); + } } diff --git a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php index 0a284558264..bf9e4a68589 100644 --- a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php @@ -139,6 +139,18 @@ public function testCloneQuery() $this->assertTrue($query->getParameters()->isEmpty()); } + public function testQueryWalkerIsKept() + { + $dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u"; + $query = $this->_em->createQuery($dql); + $query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\Tests\ORM\Functional\CustomPaginationTestTreeWalker')); + + $paginator = new Paginator($query, true); + $paginator->setUseOutputWalkers(false); + $this->assertCount(1, $paginator->getIterator()); + $this->assertEquals(1, $paginator->count()); + } + public function populate() { for ($i = 0; $i < 3; $i++) { @@ -166,3 +178,22 @@ public function useOutputWalkers() ); } } + +class CustomPaginationTestTreeWalker extends Query\TreeWalkerAdapter +{ + public function walkSelectStatement(Query\AST\SelectStatement $selectStatement) + { + $condition = new Query\AST\ConditionalPrimary(); + + $path = new Query\AST\PathExpression(Query\AST\PathExpression::TYPE_STATE_FIELD, 'u', 'name'); + $path->type = Query\AST\PathExpression::TYPE_STATE_FIELD; + + $condition->simpleConditionalExpression = new Query\AST\ComparisonExpression( + $path, + '=', + new Query\AST\Literal(Query\AST\Literal::STRING, 'Name1') + ); + + $selectStatement->whereClause = new Query\AST\WhereClause($condition); + } +}