diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index c446274d..2ae35ee1 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -480,7 +480,7 @@ public function processPendingFields($componentAlias) } $sql = array(); - foreach ($fields as $fieldName) { + foreach ($fields as $fieldAlias => $fieldName) { $columnName = $table->getColumnName($fieldName); if (($owner = $table->getColumnOwner($columnName)) !== null && $owner !== $table->getComponentName()) { @@ -492,10 +492,17 @@ public function processPendingFields($componentAlias) . ' AS ' . $this->_conn->quoteIdentifier($tableAlias . '__' . $columnName); } else { - $columnName = $table->getColumnName($fieldName); + // Fix for http://www.doctrine-project.org/jira/browse/DC-585 + // Take the field alias if available + if (isset($this->_aggregateAliasMap[$fieldAlias])) { + $aliasSql = $this->_aggregateAliasMap[$fieldAlias]; + } else { + $columnName = $table->getColumnName($fieldName); + $aliasSql = $this->_conn->quoteIdentifier($tableAlias . '__' . $columnName); + } $sql[] = $this->_conn->quoteIdentifier($tableAlias) . '.' . $this->_conn->quoteIdentifier($columnName) . ' AS ' - . $this->_conn->quoteIdentifier($tableAlias . '__' . $columnName); + . $aliasSql; } } @@ -649,6 +656,13 @@ public function parseSelect($dql) $this->_queryComponents[$componentAlias]['agg'][$index] = $alias; $this->_neededTables[] = $tableAlias; + + // Fix for http://www.doctrine-project.org/jira/browse/DC-585 + // Add selected columns to pending fields + if (preg_match('/^([^\(]+)\.(\'?)(.*?)(\'?)$/', $expression, $field)) { + $this->_pendingFields[$componentAlias][$alias] = $field[3]; + } + } else { $e = explode('.', $terms[0]);