diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index b705834e3..481118efd 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -835,22 +835,17 @@ class SqlWalker implements TreeWalker case ($assoc['type'] & ClassMetadata::TO_ONE): $conditions = array(); - foreach ($assoc['sourceToTargetKeyColumns'] as $sourceColumn => $targetColumn) { - if ($relation['isOwningSide']) { - $quotedTargetColumn = ($targetClass->containsForeignIdentifier && !isset($targetClass->fieldNames[$targetColumn])) - ? $targetColumn // Join columns cannot be quoted. - : $targetClass->getQuotedColumnName($targetClass->fieldNames[$targetColumn], $this->platform); + foreach ($assoc['joinColumns'] as $joinColumn) { + $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform); + $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform); - $conditions[] = $sourceTableAlias . '.' . $sourceColumn . ' = ' . $targetTableAlias . '.' . $quotedTargetColumn; + if ($relation['isOwningSide']) { + $conditions[] = $sourceTableAlias . '.' . $quotedSourceColumn . ' = ' . $targetTableAlias . '.' . $quotedTargetColumn; continue; } - $quotedTargetColumn = ($sourceClass->containsForeignIdentifier && !isset($sourceClass->fieldNames[$targetColumn])) - ? $targetColumn // Join columns cannot be quoted. - : $sourceClass->getQuotedColumnName($sourceClass->fieldNames[$targetColumn], $this->platform); - - $conditions[] = $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $targetTableAlias . '.' . $sourceColumn; + $conditions[] = $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $targetTableAlias . '.' . $quotedSourceColumn; } // Apply remaining inheritance restrictions @@ -878,15 +873,14 @@ class SqlWalker implements TreeWalker $conditions = array(); $relationColumns = ($relation['isOwningSide']) - ? $assoc['relationToSourceKeyColumns'] - : $assoc['relationToTargetKeyColumns']; + ? $assoc['joinTable']['joinColumns'] + : $assoc['joinTable']['inverseJoinColumns']; - foreach ($relationColumns as $relationColumn => $sourceColumn) { - $quotedTargetColumn = ($sourceClass->containsForeignIdentifier && !isset($sourceClass->fieldNames[$sourceColumn])) - ? $sourceColumn // Join columns cannot be quoted. - : $sourceClass->getQuotedColumnName($sourceClass->fieldNames[$sourceColumn], $this->platform); + foreach ($relationColumns as $joinColumn) { + $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform); + $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform); - $conditions[] = $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $relationColumn; + $conditions[] = $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $quotedSourceColumn; } $sql .= $joinTableName . ' ' . $joinTableAlias . ' ON ' . implode(' AND ', $conditions); @@ -896,15 +890,14 @@ class SqlWalker implements TreeWalker $conditions = array(); $relationColumns = ($relation['isOwningSide']) - ? $assoc['relationToTargetKeyColumns'] - : $assoc['relationToSourceKeyColumns']; + ? $assoc['joinTable']['inverseJoinColumns'] + : $assoc['joinTable']['joinColumns']; - foreach ($relationColumns as $relationColumn => $targetColumn) { - $quotedTargetColumn = ($targetClass->containsForeignIdentifier && !isset($targetClass->fieldNames[$targetColumn])) - ? $targetColumn // Join columns cannot be quoted. - : $targetClass->getQuotedColumnName($targetClass->fieldNames[$targetColumn], $this->platform); + foreach ($relationColumns as $joinColumn) { + $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform); + $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform); - $conditions[] = $targetTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $relationColumn; + $conditions[] = $targetTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $quotedSourceColumn; } // Apply remaining inheritance restrictions diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 3a6e349dd..fa5b06d82 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -1662,8 +1662,42 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase ); } -} + /** + * @group DDC-1845 + */ + public function testQuotedWalkJoinVariableDeclaration() + { + $this->assertSqlGeneration( + 'SELECT u, a FROM Doctrine\Tests\Models\Quote\User u JOIN u.address a', + 'SELECT q0_."user-id" AS userid0, q0_."user-name" AS username1, q1_."address-id" AS addressid2, q1_."address-zip" AS addresszip3 FROM "quote-user" q0_ INNER JOIN "quote-address" q1_ ON q0_."address-id" = q1_."address-id"' + ); + $this->assertSqlGeneration( + 'SELECT u, p FROM Doctrine\Tests\Models\Quote\User u JOIN u.phones p', + 'SELECT q0_."user-id" AS userid0, q0_."user-name" AS username1, q1_."phone-number" AS phonenumber2 FROM "quote-user" q0_ INNER JOIN "quote-phone" q1_ ON q0_."user-id" = q1_."user-id"' + ); + + $this->assertSqlGeneration( + 'SELECT u, g FROM Doctrine\Tests\Models\Quote\User u JOIN u.groups g', + 'SELECT q0_."user-id" AS userid0, q0_."user-name" AS username1, q1_."group-id" AS groupid2, q1_."group-name" AS groupname3 FROM "quote-user" q0_ INNER JOIN "quote-users-groups" q2_ ON q0_."user-id" = q2_."user-id" INNER JOIN "quote-group" q1_ ON q1_."group-id" = q2_."group-id"' + ); + + $this->assertSqlGeneration( + 'SELECT a, u FROM Doctrine\Tests\Models\Quote\Address a JOIN a.user u', + 'SELECT q0_."address-id" AS addressid0, q0_."address-zip" AS addresszip1, q1_."user-id" AS userid2, q1_."user-name" AS username3 FROM "quote-address" q0_ INNER JOIN "quote-user" q1_ ON q0_."user-id" = q1_."user-id"' + ); + + $this->assertSqlGeneration( + 'SELECT g, u FROM Doctrine\Tests\Models\Quote\Group g JOIN g.users u', + 'SELECT q0_."group-id" AS groupid0, q0_."group-name" AS groupname1, q1_."user-id" AS userid2, q1_."user-name" AS username3 FROM "quote-group" q0_ INNER JOIN "quote-users-groups" q2_ ON q0_."group-id" = q2_."group-id" INNER JOIN "quote-user" q1_ ON q1_."user-id" = q2_."user-id"' + ); + + $this->assertSqlGeneration( + 'SELECT g, p FROM Doctrine\Tests\Models\Quote\Group g JOIN g.parent p', + 'SELECT q0_."group-id" AS groupid0, q0_."group-name" AS groupname1, q1_."group-id" AS groupid2, q1_."group-name" AS groupname3 FROM "quote-group" q0_ INNER JOIN "quote-group" q1_ ON q0_."parent-id" = q1_."group-id"' + ); + } +} class MyAbsFunction extends \Doctrine\ORM\Query\AST\Functions\FunctionNode {