From d9bb861b1fd8741bce443898f4b1455f4424d398 Mon Sep 17 00:00:00 2001 From: Thomas Rabaix Date: Tue, 24 Jan 2012 22:51:21 +0100 Subject: [PATCH] Fix DDC-1618 - add more check before throwing an iterateWithFetchJoinNotAllowed exception --- lib/Doctrine/ORM/Query/SqlWalker.php | 11 ++++++--- tests/Doctrine/Tests/ORM/Query/QueryTest.php | 24 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index f1ef94ff1..c91eae147 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -590,6 +590,10 @@ class SqlWalker implements TreeWalker $sql = 'SELECT ' . (($selectClause->isDistinct) ? 'DISTINCT ' : ''); $sqlSelectExpressions = array_filter(array_map(array($this, 'walkSelectExpression'), $selectClause->selectExpressions)); + if ($this->_query->getHint(Query::HINT_INTERNAL_ITERATION) == true && $selectClause->isDistinct) { + $this->_query->setHint('doctrine.distinct', true); + } + $addMetaColumns = ! $this->_query->getHint(Query::HINT_FORCE_PARTIAL_LOAD) && $this->_query->getHydrationMode() == Query::HYDRATE_OBJECT || @@ -811,9 +815,10 @@ class SqlWalker implements TreeWalker // Ensure we got the owning side, since it has all mapping info $assoc = ( ! $relation['isOwningSide']) ? $targetClass->associationMappings[$relation['mappedBy']] : $relation; - - if ($this->_query->getHint(Query::HINT_INTERNAL_ITERATION) == true && $relation['type'] & ClassMetadata::TO_MANY) { - throw QueryException::iterateWithFetchJoinNotAllowed($assoc); + if ($this->_query->getHint(Query::HINT_INTERNAL_ITERATION) == true && (!$this->_query->getHint('doctrine.distinct') || isset($this->_selectedClasses[$joinedDqlAlias]))) { + if ($relation['type'] == ClassMetadata::ONE_TO_MANY || $relation['type'] == ClassMetadata::MANY_TO_MANY) { + throw QueryException::iterateWithFetchJoinNotAllowed($assoc); + } } if ($joinVarDecl->indexBy) { diff --git a/tests/Doctrine/Tests/ORM/Query/QueryTest.php b/tests/Doctrine/Tests/ORM/Query/QueryTest.php index 4e142e149..f975af3ec 100644 --- a/tests/Doctrine/Tests/ORM/Query/QueryTest.php +++ b/tests/Doctrine/Tests/ORM/Query/QueryTest.php @@ -101,4 +101,28 @@ class QueryTest extends \Doctrine\Tests\OrmTestCase $q->useResultCache(true); $this->assertSame($this->_em->getConfiguration()->getResultCacheImpl(), $q->getQueryCacheProfile()->getResultCacheDriver()); } + + /** + * @expectedException Doctrine\ORM\Query\QueryException + **/ + public function testIterateWithNoDistinctAndWrongSelectClause() + { + $q = $this->_em->createQuery("select u, a from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a"); + $q->iterate(); + } + + /** + * @expectedException Doctrine\ORM\Query\QueryException + **/ + public function testIterateWithNoDistinctAndWithValidSelectClause() + { + $q = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a"); + $q->iterate(); + } + + public function testIterateWithDistinct() + { + $q = $this->_em->createQuery("SELECT DISTINCT u from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a"); + $q->iterate(); + } }