1
0
mirror of synced 2025-02-02 13:31:45 +03:00

Fix applying ON/WITH conditions to first join in Class Table Inheritance

Now we build nested joins for CTI when using ON/WITH clause.
This commit is contained in:
Strate 2014-01-19 20:56:24 +04:00 committed by Vitaliy Berdylo
parent b11ae45a2f
commit 8d33ccced1
2 changed files with 26 additions and 7 deletions

View File

@ -892,6 +892,8 @@ class SqlWalker implements TreeWalker
} }
} }
$targetTableJoin = null;
// This condition is not checking ClassMetadata::MANY_TO_ONE, because by definition it cannot // This condition is not checking ClassMetadata::MANY_TO_ONE, because by definition it cannot
// be the owning side and previously we ensured that $assoc is always the owning side of the associations. // be the owning side and previously we ensured that $assoc is always the owning side of the associations.
// The owning side is necessary at this point because only it contains the JoinColumn information. // The owning side is necessary at this point because only it contains the JoinColumn information.
@ -926,7 +928,10 @@ class SqlWalker implements TreeWalker
$conditions[] = $filterExpr; $conditions[] = $filterExpr;
} }
$sql .= $targetTableName . ' ' . $targetTableAlias . ' ON ' . implode(' AND ', $conditions); $targetTableJoin = array(
'table' => $targetTableName . ' ' . $targetTableAlias,
'condition' => implode(' AND ', $conditions),
);
break; break;
case ($assoc['type'] == ClassMetadata::MANY_TO_MANY): case ($assoc['type'] == ClassMetadata::MANY_TO_MANY):
@ -978,15 +983,29 @@ class SqlWalker implements TreeWalker
$conditions[] = $filterExpr; $conditions[] = $filterExpr;
} }
$sql .= $targetTableName . ' ' . $targetTableAlias . ' ON ' . implode(' AND ', $conditions); $targetTableJoin = array(
'table' => $targetTableName . ' ' . $targetTableAlias,
'condition' => implode(' AND ', $conditions),
);
break; break;
} }
// Handle WITH clause // Handle WITH clause
if ($condExpr !== null) { $withCondition = (null !== $condExpr) ? ('(' . $this->walkConditionalExpression($condExpr) . ')') : '';
// Phase 2 AST optimization: Skip processing of ConditionalExpression
// if only one ConditionalTerm is defined if ($targetClass->isInheritanceTypeJoined()) {
$sql .= ' AND (' . $this->walkConditionalExpression($condExpr) . ')'; $ctiJoins = $this->_generateClassTableInheritanceJoins($targetClass, $joinedDqlAlias);
// If we have WITH condition, we need to build nested joins for target class table and cti joins
if ($withCondition) {
$sql .= '(' . $targetTableJoin['table'] . $ctiJoins . ') ON ' . $targetTableJoin['condition'] . ' AND ' . $withCondition;
} else {
$sql .= $targetTableJoin['table'] . ' ON ' . $targetTableJoin['condition'] . $ctiJoins;
}
} else {
$sql .= $targetTableJoin['table'] . ' ON ' . $targetTableJoin['condition'];
if ($withCondition) {
$sql .= ' AND ' . $withCondition;
}
} }
// FIXME: these should either be nested or all forced to be left joins (DDC-XXX) // FIXME: these should either be nested or all forced to be left joins (DDC-XXX)

View File

@ -2058,7 +2058,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
{ {
$this->assertSqlGeneration( $this->assertSqlGeneration(
'SELECT e.id FROM Doctrine\Tests\Models\Company\CompanyOrganization o JOIN o.events e WITH e.id = ?1', 'SELECT e.id FROM Doctrine\Tests\Models\Company\CompanyOrganization o JOIN o.events e WITH e.id = ?1',
'SELECT c0_.id AS id0 FROM company_organizations c1_ INNER JOIN company_events c0_ ON c1_.id = c0_.org_id AND (c0_.id = ?) LEFT JOIN company_auctions c2_ ON c0_.id = c2_.id LEFT JOIN company_raffles c3_ ON c0_.id = c3_.id', 'SELECT c0_.id AS id0 FROM company_organizations c1_ INNER JOIN (company_events c0_ LEFT JOIN company_auctions c2_ ON c0_.id = c2_.id LEFT JOIN company_raffles c3_ ON c0_.id = c3_.id) ON c1_.id = c0_.org_id AND (c0_.id = ?)',
array(Query::HINT_FORCE_PARTIAL_LOAD => false) array(Query::HINT_FORCE_PARTIAL_LOAD => false)
); );
} }