From 55969999c6a5f17cd3b1a6e51c43242060d0dc67 Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Mon, 15 Apr 2024 20:28:37 +0200 Subject: [PATCH] Revert "Revert "Added fields with alias to pendingFields array, fixed DC-585"" This reverts commit 29961c70864cf56c69c0f08740406067cf0bf114. --- lib/Doctrine/Query.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index c446274d3..2ae35ee15 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]);