diff --git a/lib/Doctrine/ORM/Query/AST/Functions/SizeFunction.php b/lib/Doctrine/ORM/Query/AST/Functions/SizeFunction.php index 601c4f804..c66df6a8b 100644 --- a/lib/Doctrine/ORM/Query/AST/Functions/SizeFunction.php +++ b/lib/Doctrine/ORM/Query/AST/Functions/SizeFunction.php @@ -53,8 +53,8 @@ class SizeFunction extends FunctionNode if ($assoc['type'] == \Doctrine\ORM\Mapping\ClassMetadata::ONE_TO_MANY) { $targetClass = $sqlWalker->getEntityManager()->getClassMetadata($assoc['targetEntity']); - $targetTableAlias = $sqlWalker->getSqlTableAlias($targetClass->table['name']); - $sourceTableAlias = $sqlWalker->getSqlTableAlias($class->table['name'], $dqlAlias); + $targetTableAlias = $sqlWalker->getSQLTableAlias($targetClass->table['name']); + $sourceTableAlias = $sqlWalker->getSQLTableAlias($class->table['name'], $dqlAlias); $sql .= $targetClass->getQuotedTableName($platform) . ' ' . $targetTableAlias . ' WHERE '; @@ -76,8 +76,8 @@ class SizeFunction extends FunctionNode $joinTable = $owningAssoc['joinTable']; // SQL table aliases - $joinTableAlias = $sqlWalker->getSqlTableAlias($joinTable['name']); - $sourceTableAlias = $sqlWalker->getSqlTableAlias($class->table['name'], $dqlAlias); + $joinTableAlias = $sqlWalker->getSQLTableAlias($joinTable['name']); + $sourceTableAlias = $sqlWalker->getSQLTableAlias($class->table['name'], $dqlAlias); // join to target table $sql .= $targetClass->getQuotedJoinTableName($owningAssoc, $platform) . ' ' . $joinTableAlias . ' WHERE '; diff --git a/lib/Doctrine/ORM/Query/Exec/MultiTableDeleteExecutor.php b/lib/Doctrine/ORM/Query/Exec/MultiTableDeleteExecutor.php index 8ddb78fff..e8bb20212 100644 --- a/lib/Doctrine/ORM/Query/Exec/MultiTableDeleteExecutor.php +++ b/lib/Doctrine/ORM/Query/Exec/MultiTableDeleteExecutor.php @@ -63,9 +63,11 @@ class MultiTableDeleteExecutor extends AbstractSqlExecutor $idColumnList = implode(', ', $idColumnNames); // 1. Create an INSERT INTO temptable ... SELECT identifiers WHERE $AST->getWhereClause() + $sqlWalker->setSQLTableAlias($primaryClass->table['name'], 't0', $primaryDqlAlias); + $this->_insertSql = 'INSERT INTO ' . $tempTable . ' (' . $idColumnList . ')' . ' SELECT t0.' . implode(', t0.', $idColumnNames); - $sqlWalker->setSqlTableAlias($primaryClass->table['name'] . $primaryDqlAlias, 't0'); + $rangeDecl = new AST\RangeVariableDeclaration($primaryClass->name, $primaryDqlAlias); $fromClause = new AST\FromClause(array(new AST\IdentificationVariableDeclaration($rangeDecl, null, array()))); $this->_insertSql .= $sqlWalker->walkFromClause($fromClause); diff --git a/lib/Doctrine/ORM/Query/Exec/MultiTableUpdateExecutor.php b/lib/Doctrine/ORM/Query/Exec/MultiTableUpdateExecutor.php index 7298db976..d5cd9fb81 100644 --- a/lib/Doctrine/ORM/Query/Exec/MultiTableUpdateExecutor.php +++ b/lib/Doctrine/ORM/Query/Exec/MultiTableUpdateExecutor.php @@ -51,7 +51,7 @@ class MultiTableUpdateExecutor extends AbstractSqlExecutor $em = $sqlWalker->getEntityManager(); $conn = $em->getConnection(); $platform = $conn->getDatabasePlatform(); - + $updateClause = $AST->updateClause; $primaryClass = $sqlWalker->getEntityManager()->getClassMetadata($updateClause->abstractSchemaName); @@ -64,11 +64,14 @@ class MultiTableUpdateExecutor extends AbstractSqlExecutor $idColumnList = implode(', ', $idColumnNames); // 1. Create an INSERT INTO temptable ... SELECT identifiers WHERE $AST->getWhereClause() + $sqlWalker->setSQLTableAlias($primaryClass->table['name'], 't0', $updateClause->aliasIdentificationVariable); + $this->_insertSql = 'INSERT INTO ' . $tempTable . ' (' . $idColumnList . ')' . ' SELECT t0.' . implode(', t0.', $idColumnNames); - $sqlWalker->setSqlTableAlias($primaryClass->table['name'] . $updateClause->aliasIdentificationVariable, 't0'); + $rangeDecl = new AST\RangeVariableDeclaration($primaryClass->name, $updateClause->aliasIdentificationVariable); $fromClause = new AST\FromClause(array(new AST\IdentificationVariableDeclaration($rangeDecl, null, array()))); + $this->_insertSql .= $sqlWalker->walkFromClause($fromClause); // 2. Create ID subselect statement used in UPDATE ... WHERE ... IN (subselect) @@ -85,8 +88,9 @@ class MultiTableUpdateExecutor extends AbstractSqlExecutor foreach ($updateItems as $updateItem) { $field = $updateItem->pathExpression->field; + if (isset($class->fieldMappings[$field]) && ! isset($class->fieldMappings[$field]['inherited']) || - isset($class->associationMappings[$field]) && ! isset($class->associationMappings[$field]['inherited'])) { + isset($class->associationMappings[$field]) && ! isset($class->associationMappings[$field]['inherited'])) { $newValue = $updateItem->newValue; if ( ! $affected) { @@ -103,6 +107,7 @@ class MultiTableUpdateExecutor extends AbstractSqlExecutor if ($newValue instanceof AST\InputParameter) { $paramKey = $newValue->name; $this->_sqlParameters[$i][] = $sqlWalker->getQuery()->getParameter($paramKey); + ++$this->_numParametersInUpdateClause; } } @@ -120,14 +125,17 @@ class MultiTableUpdateExecutor extends AbstractSqlExecutor // 4. Store DDL for temporary identifier table. $columnDefinitions = array(); + foreach ($idColumnNames as $idColumnName) { $columnDefinitions[$idColumnName] = array( 'notnull' => true, 'type' => Type::getType($rootClass->getTypeOfColumn($idColumnName)) ); } + $this->_createTempTableSql = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' (' . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')'; + $this->_dropTempTableSql = 'DROP TABLE ' . $tempTable; } diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index f26b6d151..d94d50b30 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -194,7 +194,7 @@ class SqlWalker implements TreeWalker */ public function getSQLTableAlias($tableName, $dqlAlias = '') { - $tableName .= $dqlAlias; + $tableName .= ($dqlAlias) ? '@[' . $dqlAlias . ']' : ''; if ( ! isset($this->_tableAliasMap[$tableName])) { $this->_tableAliasMap[$tableName] = strtolower(substr($tableName, 0, 1)) . $this->_tableAliasCounter++ . '_'; @@ -214,7 +214,7 @@ class SqlWalker implements TreeWalker */ public function setSQLTableAlias($tableName, $alias, $dqlAlias = '') { - $tableName .= $dqlAlias; + $tableName .= ($dqlAlias) ? '@[' . $dqlAlias . ']' : ''; $this->_tableAliasMap[$tableName] = $alias;