Merge pull request #886 from Strate/bugfix/join-with-condition-placement-fix
[DDC-1256] Fix applying ON/WITH conditions to first join in Class Table Inheritance
This commit is contained in:
commit
0dbd742588
@ -894,6 +894,8 @@ class SqlWalker implements TreeWalker
|
||||
}
|
||||
}
|
||||
|
||||
$targetTableJoin = null;
|
||||
|
||||
// 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.
|
||||
// The owning side is necessary at this point because only it contains the JoinColumn information.
|
||||
@ -928,7 +930,10 @@ class SqlWalker implements TreeWalker
|
||||
$conditions[] = $filterExpr;
|
||||
}
|
||||
|
||||
$sql .= $targetTableName . ' ' . $targetTableAlias . ' ON ' . implode(' AND ', $conditions);
|
||||
$targetTableJoin = array(
|
||||
'table' => $targetTableName . ' ' . $targetTableAlias,
|
||||
'condition' => implode(' AND ', $conditions),
|
||||
);
|
||||
break;
|
||||
|
||||
case ($assoc['type'] == ClassMetadata::MANY_TO_MANY):
|
||||
@ -980,20 +985,33 @@ class SqlWalker implements TreeWalker
|
||||
$conditions[] = $filterExpr;
|
||||
}
|
||||
|
||||
$sql .= $targetTableName . ' ' . $targetTableAlias . ' ON ' . implode(' AND ', $conditions);
|
||||
$targetTableJoin = array(
|
||||
'table' => $targetTableName . ' ' . $targetTableAlias,
|
||||
'condition' => implode(' AND ', $conditions),
|
||||
);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \BadMethodCallException('Type of association must be one of *_TO_ONE or MANY_TO_MANY');
|
||||
}
|
||||
|
||||
// Handle WITH clause
|
||||
if ($condExpr !== null) {
|
||||
// Phase 2 AST optimization: Skip processing of ConditionalExpression
|
||||
// if only one ConditionalTerm is defined
|
||||
$sql .= ' AND (' . $this->walkConditionalExpression($condExpr) . ')';
|
||||
$withCondition = (null === $condExpr) ? '' : ('(' . $this->walkConditionalExpression($condExpr) . ')');
|
||||
|
||||
if ($targetClass->isInheritanceTypeJoined()) {
|
||||
$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'];
|
||||
} else {
|
||||
$sql .= $targetTableJoin['table'] . ' ON ' . $targetTableJoin['condition'] . $ctiJoins;
|
||||
}
|
||||
} else {
|
||||
$sql .= $targetTableJoin['table'] . ' ON ' . $targetTableJoin['condition'];
|
||||
}
|
||||
|
||||
// FIXME: these should either be nested or all forced to be left joins (DDC-XXX)
|
||||
if ($targetClass->isInheritanceTypeJoined()) {
|
||||
$sql .= $this->_generateClassTableInheritanceJoins($targetClass, $joinedDqlAlias);
|
||||
if ($withCondition) {
|
||||
$sql .= ' AND ' . $withCondition;
|
||||
}
|
||||
|
||||
// Apply the indexes
|
||||
|
@ -2065,7 +2065,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
|
||||
{
|
||||
$this->assertSqlGeneration(
|
||||
'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)
|
||||
);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user