From ad115f0ac8e6fa52b3d923aad6a3fa3e6f497b6b Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Mon, 26 Jul 2010 01:13:03 -0300 Subject: [PATCH] [DDC-698] Fixed wrong SQL generation of m2m with starting path on inverse side. --- lib/Doctrine/ORM/Query/SqlWalker.php | 8 +- .../ORM/Functional/Ticket/DDC698Test.php | 104 ++++++++++++++++++ 2 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC698Test.php diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 4f5c23325..2e9620105 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -760,7 +760,7 @@ class SqlWalker implements TreeWalker $joinTableAlias = $this->getSqlTableAlias($joinTable['name'], $joinedDqlAlias); $sql .= $assoc->getQuotedJoinTableName($this->_platform) . ' ' . $joinTableAlias . ' ON '; - $first = true; + $first = true; if ($relation->isOwningSide) { foreach ($assoc->relationToSourceKeyColumns as $relationColumn => $sourceColumn) { if ( ! $first) $sql .= ' AND '; else $first = false; @@ -773,7 +773,7 @@ class SqlWalker implements TreeWalker foreach ($assoc->relationToTargetKeyColumns as $relationColumn => $targetColumn) { if ( ! $first) $sql .= ' AND '; else $first = false; - $sql .= $sourceTableAlias . '.' . $targetClass->getQuotedColumnName($targetClass->fieldNames[$targetColumn], $this->_platform) + $sql .= $sourceTableAlias . '.' . $sourceClass->getQuotedColumnName($sourceClass->fieldNames[$targetColumn], $this->_platform) . ' = ' . $joinTableAlias . '.' . $relationColumn; } @@ -784,7 +784,7 @@ class SqlWalker implements TreeWalker ? ' LEFT JOIN ' : ' INNER JOIN '; $sql .= $targetTableName . ' ' . $targetTableAlias . ' ON '; - $first = true; + $first = true; if ($relation->isOwningSide) { foreach ($assoc->relationToTargetKeyColumns as $relationColumn => $targetColumn) { if ( ! $first) $sql .= ' AND '; else $first = false; @@ -797,7 +797,7 @@ class SqlWalker implements TreeWalker foreach ($assoc->relationToSourceKeyColumns as $relationColumn => $sourceColumn) { if ( ! $first) $sql .= ' AND '; else $first = false; - $sql .= $targetTableAlias . '.' . $sourceClass->getQuotedColumnName($sourceClass->fieldNames[$sourceColumn], $this->_platform) + $sql .= $targetTableAlias . '.' . $targetClass->getQuotedColumnName($targetClass->fieldNames[$sourceColumn], $this->_platform) . ' = ' . $joinTableAlias . '.' . $relationColumn; } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC698Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC698Test.php new file mode 100644 index 000000000..c2c20c0ce --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC698Test.php @@ -0,0 +1,104 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC698Role'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC698Privilege') + )); + } catch(\Exception $e) { + + } + } + + public function testTicket() + { + $qb = $this->_em->createQueryBuilder(); + $qb->select('p', 'r') + ->from(__NAMESPACE__ . '\DDC698Privilege', 'p') + ->leftJoin('p.roles', 'r'); + + $sql = $qb->getQuery()->getSQL(); + + $this->assertEquals('SELECT p0_.privilegeID AS privilegeID0, p0_.name AS name1, r1_.roleID AS roleID2, r1_.name AS name3, r1_.shortName AS shortName4 FROM Privileges p0_ LEFT JOIN RolePrivileges r2_ ON p0_.privilegeID = r2_.privilegeID LEFT JOIN Roles r1_ ON r1_.roleID = r2_.roleID', $sql); + } +} + +/** + * + * @Table(name="Roles") + * @Entity + */ +class DDC698Role +{ + /** + * @Id @Column(name="roleID", type="integer") + * @GeneratedValue(strategy="AUTO") + * + */ + protected $roleID; + + /** + * @Column(name="name", type="string", length="45") + * + * + */ + protected $name; + + /** + * @Column(name="shortName", type="string", length="45") + * + * + */ + protected $shortName; + + + + /** + * @ManyToMany(targetEntity="DDC698Privilege", inversedBy="roles") + * @JoinTable(name="RolePrivileges", + * joinColumns={@JoinColumn(name="roleID", referencedColumnName="roleID")}, + * inverseJoinColumns={@JoinColumn(name="privilegeID", referencedColumnName="privilegeID")} + * ) + */ + protected $privilege; + +} + + +/** + * + * @Table(name="Privileges") + * @Entity() + */ +class DDC698Privilege +{ + /** + * @Id @Column(name="privilegeID", type="integer") + * @GeneratedValue(strategy="AUTO") + * + */ + protected $privilegeID; + + /** + * @Column(name="name", type="string", length="45") + * + * + */ + protected $name; + + /** + * @ManyToMany(targetEntity="DDC698Role", mappedBy="privilege") + */ + protected $roles; +} \ No newline at end of file