From be1cc14a9c8641774d614f788103cef4a5373bb1 Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Thu, 17 Apr 2014 19:29:18 +0000 Subject: [PATCH] DDC-2890 Fixed pagination with association order by. --- lib/Doctrine/ORM/Query/Parser.php | 3 +- .../Tools/Pagination/LimitSubqueryWalker.php | 43 +++++++++++++------ .../Pagination/LimitSubqueryWalkerTest.php | 41 ++++++++++++++---- 3 files changed, 64 insertions(+), 23 deletions(-) diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index 9b7103166..cd7909dd7 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -3404,7 +3404,8 @@ class Parser * "SUBSTRING" "(" StringPrimary "," SimpleArithmeticExpression "," SimpleArithmeticExpression ")" | * "TRIM" "(" [["LEADING" | "TRAILING" | "BOTH"] [char] "FROM"] StringPrimary ")" | * "LOWER" "(" StringPrimary ")" | - * "UPPER" "(" StringPrimary ")" + * "UPPER" "(" StringPrimary ")" | + * "IDENTITY" "(" SingleValuedAssociationPathExpression {"," string} ")" * * @return \Doctrine\ORM\Query\AST\Functions\FunctionNode */ diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php index ca3f1032c..2cf5e527f 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php @@ -20,9 +20,10 @@ namespace Doctrine\ORM\Tools\Pagination; use Doctrine\DBAL\Types\Type; use Doctrine\ORM\Query\TreeWalkerAdapter; -use Doctrine\ORM\Query\AST\SelectStatement; -use Doctrine\ORM\Query\AST\SelectExpression; +use Doctrine\ORM\Query\AST\Functions\IdentityFunction; use Doctrine\ORM\Query\AST\PathExpression; +use Doctrine\ORM\Query\AST\SelectExpression; +use Doctrine\ORM\Query\AST\SelectStatement; /** * Replaces the selectClause of the AST with a SELECT DISTINCT root.id equivalent. @@ -99,21 +100,37 @@ class LimitSubqueryWalker extends TreeWalkerAdapter if (isset($AST->orderByClause)) { foreach ($AST->orderByClause->orderByItems as $item) { - if ($item->expression instanceof PathExpression) { - $pathExpression = new PathExpression( - PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, - $item->expression->identificationVariable, - $item->expression->field - ); - $pathExpression->type = PathExpression::TYPE_STATE_FIELD; - $AST->selectClause->selectExpressions[] = new SelectExpression( - $pathExpression, - '_dctrn_ord' . $this->_aliasCounter++ - ); + if ( ! $item->expression instanceof PathExpression) { + continue; } + + $AST->selectClause->selectExpressions[] = new SelectExpression( + $this->createSelectExpressionItem($item->expression), + '_dctrn_ord' . $this->_aliasCounter++ + ); } } $AST->selectClause->isDistinct = true; } + + /** + * Retrieve either an IdentityFunction (IDENTITY(u.assoc)) or a state field (u.name). + * + * @param \Doctrine\ORM\Query\AST\PathExpression $pathExpression + * + * @return \Doctrine\ORM\Query\AST\Functions\IdentityFunction + */ + private function createSelectExpressionItem(PathExpression $pathExpression) + { + if ($pathExpression->type === PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION) { + $identity = new IdentityFunction('identity'); + + $identity->pathExpression = clone $pathExpression; + + return $identity; + } + + return clone $pathExpression; + } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php index 3ad5b969b..1bb2b6463 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php @@ -11,37 +11,60 @@ class LimitSubqueryWalkerTest extends PaginationTestCase { public function testLimitSubquery() { - $query = $this->entityManager->createQuery( - 'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a'); + $dql = 'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a'; + $query = $this->entityManager->createQuery($dql); $limitQuery = clone $query; + $limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\LimitSubqueryWalker')); $this->assertEquals( - "SELECT DISTINCT m0_.id AS id_0 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id INNER JOIN Author a2_ ON m0_.author_id = a2_.id", $limitQuery->getSql() + "SELECT DISTINCT m0_.id AS id_0 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id INNER JOIN Author a2_ ON m0_.author_id = a2_.id", + $limitQuery->getSql() ); } public function testLimitSubqueryWithSort() { - $query = $this->entityManager->createQuery( - 'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a ORDER BY p.title'); + $dql = 'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a ORDER BY p.title'; + $query = $this->entityManager->createQuery($dql); $limitQuery = clone $query; + $limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\LimitSubqueryWalker')); $this->assertEquals( - "SELECT DISTINCT m0_.id AS id_0, m0_.title AS title_1 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id INNER JOIN Author a2_ ON m0_.author_id = a2_.id ORDER BY m0_.title ASC", $limitQuery->getSql() + "SELECT DISTINCT m0_.id AS id_0, m0_.title AS title_1 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id INNER JOIN Author a2_ ON m0_.author_id = a2_.id ORDER BY m0_.title ASC", + $limitQuery->getSql() ); } public function testCountQuery_MixedResultsWithName() { - $query = $this->entityManager->createQuery( - 'SELECT a, sum(a.name) as foo FROM Doctrine\Tests\ORM\Tools\Pagination\Author a'); + $dql = 'SELECT a, sum(a.name) as foo FROM Doctrine\Tests\ORM\Tools\Pagination\Author a'; + $query = $this->entityManager->createQuery($dql); $limitQuery = clone $query; + $limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\LimitSubqueryWalker')); $this->assertEquals( - "SELECT DISTINCT a0_.id AS id_0, sum(a0_.name) AS sclr_1 FROM Author a0_", $limitQuery->getSql() + "SELECT DISTINCT a0_.id AS id_0, sum(a0_.name) AS sclr_1 FROM Author a0_", + $limitQuery->getSql() + ); + } + + /** + * @group DDC-2890 + */ + public function testLimitSubqueryWithSortOnAssociation() + { + $dql = 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p ORDER BY p.author'; + $query = $this->entityManager->createQuery($dql); + $limitQuery = clone $query; + + $limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\LimitSubqueryWalker')); + + $this->assertEquals( + "SELECT DISTINCT m0_.id AS id_0, m0_.author_id AS sclr_1 FROM MyBlogPost m0_ ORDER BY m0_.author_id ASC", + $limitQuery->getSql() ); } }