From 7f1becf2832b49d842078b30f4a5c9153ee946b0 Mon Sep 17 00:00:00 2001 From: Roger Llopart Pla Date: Wed, 3 Jul 2013 12:14:16 +0200 Subject: [PATCH 1/4] Appending the Paginator tree walker hint, instead of removing all the other hints. --- lib/Doctrine/ORM/Tools/Pagination/Paginator.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) mode change 100644 => 100755 lib/Doctrine/ORM/Tools/Pagination/Paginator.php 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 2d45df95a..6e2accc7c --- a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php +++ b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php @@ -134,7 +134,7 @@ class Paginator implements \Countable, \IteratorAggregate $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 @@ class Paginator implements \Countable, \IteratorAggregate 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,16 @@ class Paginator implements \Countable, \IteratorAggregate return $this->useOutputWalkers; } + + 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); + } } From 1bc614039412de8e8aaaaf5adc0b0bc17be01c7b Mon Sep 17 00:00:00 2001 From: Roger Llopart Pla Date: Wed, 3 Jul 2013 12:24:57 +0200 Subject: [PATCH 2/4] Added docblock. --- lib/Doctrine/ORM/Tools/Pagination/Paginator.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php index 6e2accc7c..b9e8c5298 100755 --- a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php +++ b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php @@ -232,6 +232,12 @@ class Paginator implements \Countable, \IteratorAggregate 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); From e755fe7842e33d24900530c13821545a59eaefae Mon Sep 17 00:00:00 2001 From: Roger Llopart Pla Date: Wed, 3 Jul 2013 11:18:19 +0000 Subject: [PATCH 3/4] Added a test which verifies that the tree walkers are kept. --- .../Tests/ORM/Functional/PaginationTest.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php index 0a2845582..15c043d84 100644 --- a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php @@ -139,6 +139,18 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase $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\CustomTreeWalker')); + + $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 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase ); } } + +class CustomTreeWalker 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); + } +} From 5a7efa289516781888df68f07f74c0c9a208cbdb Mon Sep 17 00:00:00 2001 From: Roger Llopart Pla Date: Thu, 4 Jul 2013 08:29:46 +0000 Subject: [PATCH 4/4] Fixed name colision. --- tests/Doctrine/Tests/ORM/Functional/PaginationTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php index 15c043d84..bf9e4a685 100644 --- a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php @@ -143,7 +143,7 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase { $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\CustomTreeWalker')); + $query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\Tests\ORM\Functional\CustomPaginationTestTreeWalker')); $paginator = new Paginator($query, true); $paginator->setUseOutputWalkers(false); @@ -179,7 +179,7 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase } } -class CustomTreeWalker extends Query\TreeWalkerAdapter +class CustomPaginationTestTreeWalker extends Query\TreeWalkerAdapter { public function walkSelectStatement(Query\AST\SelectStatement $selectStatement) {