Skip to content
Create a gist now

Instantly share code, notes, and snippets.

@tarnfeld /SqlWalker.php secret
Last active

  1. tarnfeld revised this gist . 1 changed file with 22 additions and 3 deletions.
    View
    25 SqlWalker.php
    @@ -138,6 +138,13 @@ class SqlWalker implements TreeWalker
    private $rootAliases = array();
    /**
    + * Any DQL alises used for left joins
    + *
    + * @var array
    + */
    + private $leftJoinAliases = array();
    +
    + /**
    * Flag that indicates whether to generate SQL table aliases in the SQL.
    * These should only be generated for SELECT queries, not for UPDATE/DELETE.
    *
    @@ -399,8 +406,15 @@ private function _generateDiscriminatorColumnConditionSQL(array $dqlAliases)
    $values[] = $conn->quote($this->em->getClassMetadata($subclassName)->discriminatorValue);
    }
    - $sqlParts[] = (($this->useSqlTableAliases) ? $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.' : '')
    - . $class->discriminatorColumn['name'] . ' IN (' . implode(', ', $values) . ')';
    + $field = (($this->useSqlTableAliases) ? $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.' : '')
    + . $class->discriminatorColumn['name'];
    + $sql = $field . ' IN (' . implode(', ', $values) . ')';
    +
    + if (in_array($dqlAlias, $this->leftJoinAliases)) {
    + $sql = '(' . $sql . ' OR ' . $field . ' IS NULL )';
    + }
    +
    + $sqlParts[] = $sql;
    }
    $sql = implode(' AND ', $sqlParts);
    @@ -1008,6 +1022,10 @@ public function walkJoin($join)
    ? ' LEFT JOIN '
    : ' INNER JOIN ';
    + if ($joinType == AST\Join::JOIN_TYPE_LEFT) {
    + $this->leftJoinAliases[] = $joinDeclaration->aliasIdentificationVariable;
    + }
    +
    switch (true) {
    case ($joinDeclaration instanceof \Doctrine\ORM\Query\AST\RangeVariableDeclaration):
    $class = $this->em->getClassMetadata($joinDeclaration->abstractSchemaName);
    @@ -1017,6 +1035,7 @@ public function walkJoin($join)
    $sql .= $this->walkRangeVariableDeclaration($joinDeclaration)
    . $condExprConjunction . '(' . $this->walkConditionalExpression($join->conditionalExpression) . ')';
    +
    break;
    case ($joinDeclaration instanceof \Doctrine\ORM\Query\AST\JoinAssociationDeclaration):
    @@ -2208,4 +2227,4 @@ public function walkResultVariable($resultVariable)
    return $resultAlias;
    }
    -}
    +}
  2. tarnfeld created this gist .
    View
    2,211 SqlWalker.php
    2,211 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
Something went wrong with that request. Please try again.