From 005d3b432b33b7e36c807eb6e3d25ce05cadc6b3 Mon Sep 17 00:00:00 2001 From: beberlei Date: Sun, 7 Feb 2010 13:05:48 +0000 Subject: [PATCH] [2.0] DDC-163 - Missing DQL Alias prefix for many-to-many table in SqlWalker lead to duplicate prefix generation in SQL statement when a many-to-many join table is used more than once. --- lib/Doctrine/ORM/Query/SqlWalker.php | 2 +- .../ORM/Functional/Ticket/DDC163Test.php | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC163Test.php diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 4c3bd80b5..102402d0e 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -704,7 +704,7 @@ class SqlWalker implements TreeWalker } else if ($assoc->isManyToMany()) { // Join relation table $joinTable = $assoc->getJoinTable(); - $joinTableAlias = $this->getSqlTableAlias($joinTable['name']); + $joinTableAlias = $this->getSqlTableAlias($joinTable['name'], $joinedDqlAlias); $sql .= $assoc->getQuotedJoinTableName($this->_platform) . ' ' . $joinTableAlias . ' ON '; if ($relation->isOwningSide) { diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC163Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC163Test.php new file mode 100644 index 000000000..e18346c20 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC163Test.php @@ -0,0 +1,65 @@ +useModelSet('company'); + parent::setUp(); + } + + /** + * @group DDC-163 + */ + public function testQueryWithOrConditionUsingTwoRelationOnSameEntity() + { + $p1 = new CompanyPerson; + $p1->setName('p1'); + + $p2 = new CompanyPerson; + $p2->setName('p2'); + + $p3 = new CompanyPerson; + $p3->setName('p3'); + + $p4 = new CompanyPerson; + $p4->setName('p4'); + + $p1->setSpouse($p3); + $p1->addFriend($p2); + $p2->addFriend($p3); + + $p3->addFriend($p4); + + $this->_em->persist($p1); + $this->_em->persist($p2); + $this->_em->persist($p3); + $this->_em->persist($p4); + + $this->_em->flush(); + $this->_em->clear(); + + $dql = 'SELECT person.name, spouse.name, friend.name + FROM Doctrine\Tests\Models\Company\CompanyPerson person + LEFT JOIN person.spouse spouse + LEFT JOIN person.friends friend + LEFT JOIN spouse.friends spouse_friend + LEFT JOIN friend.friends friend_friend + WHERE person.name=:name AND (spouse_friend.name=:name2 OR friend_friend.name=:name2)'; + + $q = $this->_em->createQuery($dql); + $q->setParameter('name', "p1"); + $q->setParameter('name2', "p4"); + $result = $q->getScalarResult(); + + $this->assertEquals('p3', $result[0]['spouse_name']); + $this->assertEquals('p1', $result[0]['person_name']); + $this->assertEquals('p2', $result[0]['friend_name']); + } +} \ No newline at end of file