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