From 114bd2435f301f84b84dd08866bdb8960c523d72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bere=C5=88?= Date: Wed, 30 Jul 2014 09:56:22 +0200 Subject: [PATCH 1/7] Arbitrary Join count walkers solution Possible solution for Arbitrary Join problem in pagination count walkers: https://groups.google.com/forum/#!topic/doctrine-user/rpPYCDNKOU8 Added a condition to test query component against SelectStatement from clause --- lib/Doctrine/ORM/Tools/Pagination/CountWalker.php | 11 ++++++++++- lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php b/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php index 778dd3072..64914f5a6 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php @@ -57,7 +57,16 @@ class CountWalker extends TreeWalkerAdapter && $qComp['nestingLevel'] == 0 ; if ($isParent) { - $rootComponents[] = array($dqlAlias => $qComp); + foreach($AST->fromClause->identificationVariableDeclarations as $identificationVariableDeclaration) { + $isRoot = $identificationVariableDeclaration->rangeVariableDeclaration + && $identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable == $dqlAlias + && $identificationVariableDeclaration->rangeVariableDeclaration->isRoot + ; + if ($isRoot) { + $rootComponents[] = array($dqlAlias => $qComp); + break; + } + } } } if (count($rootComponents) > 1) { diff --git a/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php b/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php index 27cbc3c2e..460b8a708 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php @@ -78,7 +78,16 @@ class WhereInWalker extends TreeWalkerAdapter && $qComp['nestingLevel'] == 0 ; if ($isParent) { - $rootComponents[] = array($dqlAlias => $qComp); + foreach($AST->fromClause->identificationVariableDeclarations as $identificationVariableDeclaration) { + $isRoot = $identificationVariableDeclaration->rangeVariableDeclaration + && $identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable == $dqlAlias + && $identificationVariableDeclaration->rangeVariableDeclaration->isRoot + ; + if ($isRoot) { + $rootComponents[] = array($dqlAlias => $qComp); + break; + } + } } } if (count($rootComponents) > 1) { From a37f99f24205bdcb090e1f4ef1d56df93f6eaa49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bere=C5=88?= Date: Wed, 30 Jul 2014 15:55:14 +0200 Subject: [PATCH 2/7] Root selection according other pagination tools changed root selection in Walkers from looping queryComponents to using $AST->fromClause as other walkers have --- .../ORM/Tools/Pagination/CountWalker.php | 36 ++++++------------- .../Tools/Pagination/LimitSubqueryWalker.php | 25 ++++++------- .../ORM/Tools/Pagination/WhereInWalker.php | 36 ++++++------------- 3 files changed, 31 insertions(+), 66 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php b/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php index 64914f5a6..5294ab0a4 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php @@ -50,40 +50,24 @@ class CountWalker extends TreeWalkerAdapter throw new \RuntimeException('Cannot count query that uses a HAVING clause. Use the output walkers for pagination'); } - $rootComponents = array(); - foreach ($this->_getQueryComponents() as $dqlAlias => $qComp) { - $isParent = array_key_exists('parent', $qComp) - && $qComp['parent'] === null - && $qComp['nestingLevel'] == 0 - ; - if ($isParent) { - foreach($AST->fromClause->identificationVariableDeclarations as $identificationVariableDeclaration) { - $isRoot = $identificationVariableDeclaration->rangeVariableDeclaration - && $identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable == $dqlAlias - && $identificationVariableDeclaration->rangeVariableDeclaration->isRoot - ; - if ($isRoot) { - $rootComponents[] = array($dqlAlias => $qComp); - break; - } - } - } - } - if (count($rootComponents) > 1) { + $queryComponents = $this->_getQueryComponents(); + // Get the root entity and alias from the AST fromClause + $from = $AST->fromClause->identificationVariableDeclarations; + if (count($from) > 1) { throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction"); } - $root = reset($rootComponents); - $parentName = key($root); - $parent = current($root); - $identifierFieldName = $parent['metadata']->getSingleIdentifierFieldName(); + + $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; + $rootClass = $queryComponents[$rootAlias]['metadata']; + $identifierFieldName = $rootClass->getSingleIdentifierFieldName(); $pathType = PathExpression::TYPE_STATE_FIELD; - if (isset($parent['metadata']->associationMappings[$identifierFieldName])) { + if (isset($rootClass->associationMappings[$identifierFieldName])) { $pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION; } $pathExpression = new PathExpression( - PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $parentName, + PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $rootAlias, $identifierFieldName ); $pathExpression->type = $pathType; diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php index 2cf5e527f..c8cc4f7c0 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php @@ -60,37 +60,34 @@ class LimitSubqueryWalker extends TreeWalkerAdapter */ public function walkSelectStatement(SelectStatement $AST) { - $parent = null; - $parentName = null; + $queryComponents = $this->_getQueryComponents(); + // Get the root entity and alias from the AST fromClause + $from = $AST->fromClause->identificationVariableDeclarations; + $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; + $rootClass = $queryComponents[$rootAlias]['metadata']; $selectExpressions = array(); - foreach ($this->_getQueryComponents() as $dqlAlias => $qComp) { + foreach ($queryComponents as $dqlAlias => $qComp) { // Preserve mixed data in query for ordering. if (isset($qComp['resultVariable'])) { $selectExpressions[] = new SelectExpression($qComp['resultVariable'], $dqlAlias); continue; } - - if ($qComp['parent'] === null && $qComp['nestingLevel'] == 0) { - $parent = $qComp; - $parentName = $dqlAlias; - continue; - } } - - $identifier = $parent['metadata']->getSingleIdentifierFieldName(); - if (isset($parent['metadata']->associationMappings[$identifier])) { + + $identifier = $rootClass->getSingleIdentifierFieldName(); + if (isset($rootClass->associationMappings[$identifier])) { throw new \RuntimeException("Paginating an entity with foreign key as identifier only works when using the Output Walkers. Call Paginator#setUseOutputWalkers(true) before iterating the paginator."); } $this->_getQuery()->setHint( self::IDENTIFIER_TYPE, - Type::getType($parent['metadata']->getTypeOfField($identifier)) + Type::getType($rootClass->getTypeOfField($identifier)) ); $pathExpression = new PathExpression( PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, - $parentName, + $rootAlias, $identifier ); $pathExpression->type = PathExpression::TYPE_STATE_FIELD; diff --git a/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php b/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php index 460b8a708..936d6f103 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php @@ -71,39 +71,23 @@ class WhereInWalker extends TreeWalkerAdapter */ public function walkSelectStatement(SelectStatement $AST) { - $rootComponents = array(); - foreach ($this->_getQueryComponents() as $dqlAlias => $qComp) { - $isParent = array_key_exists('parent', $qComp) - && $qComp['parent'] === null - && $qComp['nestingLevel'] == 0 - ; - if ($isParent) { - foreach($AST->fromClause->identificationVariableDeclarations as $identificationVariableDeclaration) { - $isRoot = $identificationVariableDeclaration->rangeVariableDeclaration - && $identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable == $dqlAlias - && $identificationVariableDeclaration->rangeVariableDeclaration->isRoot - ; - if ($isRoot) { - $rootComponents[] = array($dqlAlias => $qComp); - break; - } - } - } - } - if (count($rootComponents) > 1) { + $queryComponents = $this->_getQueryComponents(); + // Get the root entity and alias from the AST fromClause + $from = $AST->fromClause->identificationVariableDeclarations; + if (count($from) > 1) { throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction"); } - $root = reset($rootComponents); - $parentName = key($root); - $parent = current($root); - $identifierFieldName = $parent['metadata']->getSingleIdentifierFieldName(); + + $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; + $rootClass = $queryComponents[$rootAlias]['metadata']; + $identifierFieldName = $rootClass->getSingleIdentifierFieldName(); $pathType = PathExpression::TYPE_STATE_FIELD; - if (isset($parent['metadata']->associationMappings[$identifierFieldName])) { + if (isset($rootClass->associationMappings[$identifierFieldName])) { $pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION; } - $pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $parentName, $identifierFieldName); + $pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $rootAlias, $identifierFieldName); $pathExpression->type = $pathType; $count = $this->_getQuery()->getHint(self::HINT_PAGINATOR_ID_COUNT); From 164352562bac6e061f33f6696adc4ad2b0fb7062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bere=C5=88?= Date: Thu, 31 Jul 2014 08:31:39 +0200 Subject: [PATCH 3/7] Added Tests for Arbitrary Join --- .../ORM/Tools/Pagination/CountWalkerTest.php | 16 ++++++++++ .../Pagination/LimitSubqueryWalkerTest.php | 31 +++++++++++++++++++ .../Tools/Pagination/WhereInWalkerTest.php | 29 +++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php index 4ef4d1630..7f36e7eae 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php @@ -90,5 +90,21 @@ class CountWalkerTest extends PaginationTestCase $query->getSql(); } + + /** + * Arbitrary Join + */ + public function testCountQueryWithArbitraryJoin() + { + $query = $this->entityManager->createQuery( + 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p LEFT JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH b.category = c'); + $query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\CountWalker')); + $query->setHint(CountWalker::HINT_DISTINCT, true); + $query->setFirstResult(null)->setMaxResults(null); + + $this->assertEquals( + "SELECT count(DISTINCT b0_.id) AS sclr_0 FROM BlogPost b0_ LEFT JOIN Category c1_ ON b0_.category_id = c1_.id", $query->getSql() + ); + } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php index 1bb2b6463..2e8d9160c 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php @@ -67,5 +67,36 @@ class LimitSubqueryWalkerTest extends PaginationTestCase $limitQuery->getSql() ); } + + /** + * Arbitrary Join + */ + public function testLimitSubqueryWithArbitraryJoin() + { + $dql = 'SELECT p, c FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH b.category = c'; + $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", + $limitQuery->getSql() + ); + } + + public function testLimitSubqueryWithSortWithArbitraryJoin() + { + $dql = 'SELECT p, c FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH b.category = c 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 ORDER BY m0_.title ASC", + $limitQuery->getSql() + ); + } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php index 7291c2786..ba7305ba9 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php @@ -121,5 +121,34 @@ class WhereInWalkerTest extends PaginationTestCase "SELECT u0_.id AS id_0, g1_.id AS id_1 FROM User u0_ INNER JOIN user_group u2_ ON u0_.id = u2_.user_id INNER JOIN groups g1_ ON g1_.id = u2_.group_id WHERE (NOT 1 = 2) AND u0_.id IN (?)", $whereInQuery->getSql() ); } + + /** + * Arbitrary Join + */ + public function testWhereInQueryWithArbitraryJoin_NoWhere() + { + $whereInQuery = $this->entityManager->createQuery( + 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH b.category = c' + ); + $whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\WhereInWalker')); + $whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); + + $this->assertEquals( + "SELECT b0_.id AS id_0 FROM BlogPost b0_ INNER JOIN Category c1_ ON b0_.category_id = c1_.id WHERE b0_.id IN (?)", $whereInQuery->getSql() + ); + } + + public function testWhereInQueryWithArbitraryJoin_SingleWhere() + { + $whereInQuery = $this->entityManager->createQuery( + 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH b.category = c WHERE 1 = 1' + ); + $whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\WhereInWalker')); + $whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); + + $this->assertEquals( + "SELECT b0_.id AS id_0 FROM BlogPost b0_ INNER JOIN Category c1_ ON b0_.category_id = c1_.id WHERE 1 = 1 AND b0_.id IN (?)", $whereInQuery->getSql() + ); + } } From 458b9535221f1b3f2a178ef605c9a631bcdad7fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bere=C5=88?= Date: Thu, 31 Jul 2014 08:43:02 +0200 Subject: [PATCH 4/7] fixed typos in tests --- tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php | 2 +- .../Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php | 4 ++-- .../Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php index 7f36e7eae..f64c77738 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php @@ -97,7 +97,7 @@ class CountWalkerTest extends PaginationTestCase public function testCountQueryWithArbitraryJoin() { $query = $this->entityManager->createQuery( - 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p LEFT JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH b.category = c'); + 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p LEFT JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH p.category = c'); $query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\CountWalker')); $query->setHint(CountWalker::HINT_DISTINCT, true); $query->setFirstResult(null)->setMaxResults(null); diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php index 2e8d9160c..213a397f1 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php @@ -73,7 +73,7 @@ class LimitSubqueryWalkerTest extends PaginationTestCase */ public function testLimitSubqueryWithArbitraryJoin() { - $dql = 'SELECT p, c FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH b.category = c'; + $dql = 'SELECT p, c FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH p.category = c'; $query = $this->entityManager->createQuery($dql); $limitQuery = clone $query; @@ -87,7 +87,7 @@ class LimitSubqueryWalkerTest extends PaginationTestCase public function testLimitSubqueryWithSortWithArbitraryJoin() { - $dql = 'SELECT p, c FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH b.category = c ORDER BY p.title'; + $dql = 'SELECT p, c FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH p.category = c ORDER BY p.title'; $query = $this->entityManager->createQuery($dql); $limitQuery = clone $query; diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php index ba7305ba9..672fc6b6b 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php @@ -128,7 +128,7 @@ class WhereInWalkerTest extends PaginationTestCase public function testWhereInQueryWithArbitraryJoin_NoWhere() { $whereInQuery = $this->entityManager->createQuery( - 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH b.category = c' + 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH p.category = c' ); $whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\WhereInWalker')); $whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); @@ -141,7 +141,7 @@ class WhereInWalkerTest extends PaginationTestCase public function testWhereInQueryWithArbitraryJoin_SingleWhere() { $whereInQuery = $this->entityManager->createQuery( - 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH b.category = c WHERE 1 = 1' + 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH p.category = c WHERE 1 = 1' ); $whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\WhereInWalker')); $whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); From 54ac6a0535dc9257da44b9824cdfb072292a00b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bere=C5=88?= Date: Thu, 31 Jul 2014 09:01:58 +0200 Subject: [PATCH 5/7] Updated Asset queries Missing braces and selected fields --- tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php | 2 +- .../Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php | 4 ++-- .../Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php index f64c77738..e44248926 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php @@ -103,7 +103,7 @@ class CountWalkerTest extends PaginationTestCase $query->setFirstResult(null)->setMaxResults(null); $this->assertEquals( - "SELECT count(DISTINCT b0_.id) AS sclr_0 FROM BlogPost b0_ LEFT JOIN Category c1_ ON b0_.category_id = c1_.id", $query->getSql() + "SELECT count(DISTINCT b0_.id) AS sclr_0 FROM BlogPost b0_ LEFT JOIN Category c1_ ON (b0_.category_id = c1_.id)", $query->getSql() ); } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php index 213a397f1..597ec1350 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php @@ -80,7 +80,7 @@ class LimitSubqueryWalkerTest extends PaginationTestCase $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", + "SELECT DISTINCT m0_.id AS id_0 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON (m0_.category_id = c1_.id)", $limitQuery->getSql() ); } @@ -94,7 +94,7 @@ class LimitSubqueryWalkerTest extends PaginationTestCase $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 ORDER BY m0_.title ASC", + "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) ORDER BY m0_.title ASC", $limitQuery->getSql() ); } diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php index 672fc6b6b..41152dc5d 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php @@ -134,7 +134,7 @@ class WhereInWalkerTest extends PaginationTestCase $whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); $this->assertEquals( - "SELECT b0_.id AS id_0 FROM BlogPost b0_ INNER JOIN Category c1_ ON b0_.category_id = c1_.id WHERE b0_.id IN (?)", $whereInQuery->getSql() + "SELECT b0_.id AS id_0, b0_.author_id AS author_id_1, b0_.category_id AS category_id_2 FROM BlogPost b0_ INNER JOIN Category c1_ ON (b0_.category_id = c1_.id) WHERE b0_.id IN (?)", $whereInQuery->getSql() ); } @@ -147,7 +147,7 @@ class WhereInWalkerTest extends PaginationTestCase $whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); $this->assertEquals( - "SELECT b0_.id AS id_0 FROM BlogPost b0_ INNER JOIN Category c1_ ON b0_.category_id = c1_.id WHERE 1 = 1 AND b0_.id IN (?)", $whereInQuery->getSql() + "SELECT b0_.id AS id_0, b0_.author_id AS author_id_1, b0_.category_id AS category_id_2 FROM BlogPost b0_ INNER JOIN Category c1_ ON (b0_.category_id = c1_.id) WHERE 1 = 1 AND b0_.id IN (?)", $whereInQuery->getSql() ); } } From d48be34696176666cd2cc6d841158d790910c638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bere=C5=88?= Date: Thu, 28 Aug 2014 13:17:25 +0200 Subject: [PATCH 6/7] CS Fixes --- lib/Doctrine/ORM/Tools/Pagination/CountWalker.php | 5 +++-- lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php | 3 ++- lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php | 5 +++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php b/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php index 5294ab0a4..0aa6aebfc 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php @@ -53,12 +53,13 @@ class CountWalker extends TreeWalkerAdapter $queryComponents = $this->_getQueryComponents(); // Get the root entity and alias from the AST fromClause $from = $AST->fromClause->identificationVariableDeclarations; + if (count($from) > 1) { throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction"); } - $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; - $rootClass = $queryComponents[$rootAlias]['metadata']; + $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; + $rootClass = $queryComponents[$rootAlias]['metadata']; $identifierFieldName = $rootClass->getSingleIdentifierFieldName(); $pathType = PathExpression::TYPE_STATE_FIELD; diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php index c8cc4f7c0..342e309b1 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php @@ -62,7 +62,7 @@ class LimitSubqueryWalker extends TreeWalkerAdapter { $queryComponents = $this->_getQueryComponents(); // Get the root entity and alias from the AST fromClause - $from = $AST->fromClause->identificationVariableDeclarations; + $from = $AST->fromClause->identificationVariableDeclarations; $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; $rootClass = $queryComponents[$rootAlias]['metadata']; $selectExpressions = array(); @@ -76,6 +76,7 @@ class LimitSubqueryWalker extends TreeWalkerAdapter } $identifier = $rootClass->getSingleIdentifierFieldName(); + if (isset($rootClass->associationMappings[$identifier])) { throw new \RuntimeException("Paginating an entity with foreign key as identifier only works when using the Output Walkers. Call Paginator#setUseOutputWalkers(true) before iterating the paginator."); } diff --git a/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php b/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php index 936d6f103..2584b8ef9 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php @@ -74,12 +74,13 @@ class WhereInWalker extends TreeWalkerAdapter $queryComponents = $this->_getQueryComponents(); // Get the root entity and alias from the AST fromClause $from = $AST->fromClause->identificationVariableDeclarations; + if (count($from) > 1) { throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction"); } - $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; - $rootClass = $queryComponents[$rootAlias]['metadata']; + $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; + $rootClass = $queryComponents[$rootAlias]['metadata']; $identifierFieldName = $rootClass->getSingleIdentifierFieldName(); $pathType = PathExpression::TYPE_STATE_FIELD; From 589d26fc5e57a4950d106f5776fdaf066ff2f4ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bere=C5=88?= Date: Tue, 2 Sep 2014 08:39:29 +0200 Subject: [PATCH 7/7] Changed calling $from[0] to using reset($from) changed usage $from[0] according suggestion from @Ocramius to use array reset function --- lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.php | 3 ++- lib/Doctrine/ORM/Tools/Pagination/CountWalker.php | 3 ++- .../ORM/Tools/Pagination/LimitSubqueryOutputWalker.php | 3 ++- lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php | 3 ++- lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.php b/lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.php index a65c73328..e44be6c57 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.php @@ -96,7 +96,8 @@ class CountOutputWalker extends SqlWalker throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction"); } - $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; + $fromRoot = reset($from); + $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; $rootClass = $this->queryComponents[$rootAlias]['metadata']; $rootIdentifier = $rootClass->identifier; diff --git a/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php b/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php index 0aa6aebfc..ff0c6b363 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php @@ -58,7 +58,8 @@ class CountWalker extends TreeWalkerAdapter throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction"); } - $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; + $fromRoot = reset($from); + $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; $rootClass = $queryComponents[$rootAlias]['metadata']; $identifierFieldName = $rootClass->getSingleIdentifierFieldName(); diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php index 1b32fc97c..45a6ce78b 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php @@ -122,7 +122,8 @@ class LimitSubqueryOutputWalker extends SqlWalker throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction"); } - $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; + $fromRoot = reset($from); + $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; $rootClass = $this->queryComponents[$rootAlias]['metadata']; $rootIdentifier = $rootClass->identifier; diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php index 342e309b1..c88191153 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php @@ -63,7 +63,8 @@ class LimitSubqueryWalker extends TreeWalkerAdapter $queryComponents = $this->_getQueryComponents(); // Get the root entity and alias from the AST fromClause $from = $AST->fromClause->identificationVariableDeclarations; - $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; + $fromRoot = reset($from); + $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; $rootClass = $queryComponents[$rootAlias]['metadata']; $selectExpressions = array(); diff --git a/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php b/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php index 2584b8ef9..9f42a1eea 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php @@ -79,7 +79,8 @@ class WhereInWalker extends TreeWalkerAdapter throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction"); } - $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; + $fromRoot = reset($from); + $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; $rootClass = $queryComponents[$rootAlias]['metadata']; $identifierFieldName = $rootClass->getSingleIdentifierFieldName();