Skip to content

Commit b0df112

Browse files
committed
Revert "restore previous code with issue column added twice with custom aliases"
This reverts commit 4dd7c86d723946f547bdb19976a9a1b9b5dc66ff.
1 parent fc58224 commit b0df112

File tree

4 files changed

+103
-23
lines changed

4 files changed

+103
-23
lines changed

lib/Doctrine/Hydrator/ArrayDriver.php

+34-1
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,37 @@ public function setLastElement(&$prev, &$coll, $index, $dqlAlias, $oneToOne)
8989
}
9090
}
9191
}
92-
}
92+
93+
protected function beforeAddingAggregateValue($rowData, $cache, $dqlAlias, $value)
94+
{
95+
$rowData = $this->addSelectedRelationToRowData($rowData, $dqlAlias, $cache);
96+
97+
$rowData = $this->addIdentifierColumnToRowData($rowData, $cache, $dqlAlias, $value);
98+
99+
return $rowData;
100+
}
101+
102+
private function addSelectedRelationToRowData($rowData, $dqlAlias, $cache)
103+
{
104+
if (!isset($rowData[$dqlAlias])) {
105+
if ($cache['isRelation']) {
106+
$rowData[$dqlAlias] = array();
107+
108+
foreach ($cache['identifiers'] as $identifierField) {
109+
$rowData[$dqlAlias][$identifierField] = null;
110+
}
111+
}
112+
}
113+
114+
return $rowData;
115+
}
116+
117+
private function addIdentifierColumnToRowData($rowData, $cache, $dqlAlias, $value)
118+
{
119+
if ($cache['isIdentifier'] && !isset($rowData[$dqlAlias][$cache['columnName']])) {
120+
$rowData[$dqlAlias][$cache['columnName']] = $value;
121+
}
122+
123+
return $rowData;
124+
}
125+
}

lib/Doctrine/Hydrator/Graph.php

+31-7
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,31 @@ protected function _gatherRowData(&$data, &$cache, &$id, &$nonemptyComponents)
311311
$table = $this->_queryComponents[$cache[$key]['dqlAlias']]['table'];
312312
$fieldName = $table->getFieldName($last);
313313
$cache[$key]['fieldName'] = $fieldName;
314-
if ($table->isIdentifier($fieldName)) {
314+
315+
$cache[$key]['identifiers'] = (array) $table->getIdentifier();
316+
317+
$cache[$key]['isRelation'] = isset($this->_queryComponents[$cache[$key]['dqlAlias']]['relation']);
318+
319+
if (isset($this->_queryComponents[$cache[$key]['dqlAlias']]['agg'][$fieldName])) {
320+
$cache[$key]['isAgg'] = true;
321+
$cache[$key]['aliasName'] = $this->_queryComponents[$cache[$key]['dqlAlias']]['agg'][$fieldName];
322+
} else {
323+
$cache[$key]['isAgg'] = false;
324+
$cache[$key]['aliasName'] = $fieldName;
325+
}
326+
327+
if (isset($this->_queryComponents[$cache[$key]['dqlAlias']]['agg_field'][$last])) {
328+
$cache[$key]['columnName'] = $this->_queryComponents[$cache[$key]['dqlAlias']]['agg_field'][$last];
329+
} else {
330+
$cache[$key]['columnName'] = $fieldName;
331+
}
332+
333+
if ($table->isIdentifier($cache[$key]['columnName'])) {
315334
$cache[$key]['isIdentifier'] = true;
316335
} else {
317336
$cache[$key]['isIdentifier'] = false;
318337
}
338+
319339
$type = $table->getTypeOfColumn($last);
320340
if ($type == 'integer' || $type == 'string') {
321341
$cache[$key]['isSimpleType'] = true;
@@ -328,12 +348,8 @@ protected function _gatherRowData(&$data, &$cache, &$id, &$nonemptyComponents)
328348
$map = $this->_queryComponents[$cache[$key]['dqlAlias']];
329349
$table = $map['table'];
330350
$dqlAlias = $cache[$key]['dqlAlias'];
331-
$fieldName = $cache[$key]['fieldName'];
332-
$agg = false;
333-
if (isset($this->_queryComponents[$dqlAlias]['agg'][$fieldName])) {
334-
$fieldName = $this->_queryComponents[$dqlAlias]['agg'][$fieldName];
335-
$agg = true;
336-
}
351+
$fieldName = $cache[$key]['aliasName'];
352+
$agg = $cache[$key]['isAgg'];
337353

338354
if ($cache[$key]['isIdentifier']) {
339355
$id[$dqlAlias] .= '|' . $value;
@@ -349,7 +365,10 @@ protected function _gatherRowData(&$data, &$cache, &$id, &$nonemptyComponents)
349365
// Hydrate aggregates in to the root component as well.
350366
// So we know that all aggregate values will always be available in the root component
351367
if ($agg) {
368+
$rowData = $this->beforeAddingAggregateValue($rowData, $cache[$key], $dqlAlias, $preparedValue);
369+
352370
$rowData[$this->_rootAlias][$fieldName] = $preparedValue;
371+
353372
if (isset($rowData[$dqlAlias])) {
354373
$rowData[$dqlAlias][$fieldName] = $preparedValue;
355374
}
@@ -365,6 +384,11 @@ protected function _gatherRowData(&$data, &$cache, &$id, &$nonemptyComponents)
365384
return $rowData;
366385
}
367386

387+
protected function beforeAddingAggregateValue($rowData, $cache, $dqlAlias, $value)
388+
{
389+
return $rowData;
390+
}
391+
368392
abstract public function getElementCollection($component);
369393

370394
abstract public function registerCollection($coll);

lib/Doctrine/Query.php

+22-15
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ public function processPendingFields($componentAlias)
480480
}
481481

482482
$sql = array();
483-
foreach ($fields as $fieldAlias => $fieldName) {
483+
foreach ($fields as $fieldName) {
484484
$columnName = $table->getColumnName($fieldName);
485485
if (($owner = $table->getColumnOwner($columnName)) !== null &&
486486
$owner !== $table->getComponentName()) {
@@ -492,17 +492,10 @@ public function processPendingFields($componentAlias)
492492
. ' AS '
493493
. $this->_conn->quoteIdentifier($tableAlias . '__' . $columnName);
494494
} else {
495-
// Fix for http://www.doctrine-project.org/jira/browse/DC-585
496-
// Take the field alias if available
497-
if (isset($this->_aggregateAliasMap[$fieldAlias])) {
498-
$aliasSql = $this->_aggregateAliasMap[$fieldAlias];
499-
} else {
500-
$columnName = $table->getColumnName($fieldName);
501-
$aliasSql = $this->_conn->quoteIdentifier($tableAlias . '__' . $columnName);
502-
}
495+
$columnName = $table->getColumnName($fieldName);
503496
$sql[] = $this->_conn->quoteIdentifier($tableAlias) . '.' . $this->_conn->quoteIdentifier($columnName)
504497
. ' AS '
505-
. $aliasSql;
498+
. $this->_conn->quoteIdentifier($tableAlias . '__' . $columnName);
506499
}
507500
}
508501

@@ -655,14 +648,11 @@ public function parseSelect($dql)
655648

656649
$this->_queryComponents[$componentAlias]['agg'][$index] = $alias;
657650

658-
$this->_neededTables[] = $tableAlias;
659-
660-
// Fix for http://www.doctrine-project.org/jira/browse/DC-585
661-
// Add selected columns to pending fields
662651
if (preg_match('/^([^\(]+)\.(\'?)(.*?)(\'?)$/', $expression, $field)) {
663-
$this->_pendingFields[$componentAlias][$alias] = $field[3];
652+
$this->_queryComponents[$componentAlias]['agg_field'][$index] = $field[3];
664653
}
665654

655+
$this->_neededTables[] = $tableAlias;
666656
} else {
667657
$e = explode('.', $terms[0]);
668658

@@ -678,6 +668,23 @@ public function parseSelect($dql)
678668
$this->_pendingFields[$componentAlias][] = $field;
679669
}
680670
}
671+
672+
$this->appendRelationIdentifierOnSqlSelect();
673+
}
674+
675+
private function appendRelationIdentifierOnSqlSelect()
676+
{
677+
if (Doctrine_Core::HYDRATE_ARRAY === $this->_hydrator->getHydrationMode()) {
678+
foreach ($this->_queryComponents as $componentAlias => $queryComponent) {
679+
if (isset($queryComponent['relation']) && isset($queryComponent['agg'])) {
680+
$table = $queryComponent['table'];
681+
682+
foreach ((array) $table->getIdentifier() as $field) {
683+
$this->_pendingFields[$componentAlias][] = $field;
684+
}
685+
}
686+
}
687+
}
681688
}
682689

683690
/**

lib/Doctrine/Query/Abstract.php

+16
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,16 @@ abstract class Doctrine_Query_Abstract
206206
*
207207
* map the name of the column / aggregate value this
208208
* component is mapped to a collection
209+
*
210+
* agg_field the field names for each aggregates
211+
* Example:
212+
* DQL: COMPONENT.FIELD as ALIAS
213+
* SQL: TABLE.COLUMN as TABLE__0
214+
* $_queryComponents
215+
* agg:
216+
* 0: ALIAS
217+
* agg_field:
218+
* 0: FIELD
209219
*/
210220
protected $_queryComponents = array();
211221

@@ -1259,6 +1269,9 @@ protected function _constructQueryFromCache($cached)
12591269
if (isset($components['agg'])) {
12601270
$queryComponents[$alias]['agg'] = $components['agg'];
12611271
}
1272+
if (isset($components['agg_field'])) {
1273+
$queryComponents[$alias]['agg_field'] = $components['agg_field'];
1274+
}
12621275
if (isset($components['map'])) {
12631276
$queryComponents[$alias]['map'] = $components['map'];
12641277
}
@@ -1289,6 +1302,9 @@ public function getCachedForm($customComponent = null)
12891302
if (isset($components['agg'])) {
12901303
$componentInfo[$alias]['agg'] = $components['agg'];
12911304
}
1305+
if (isset($components['agg_field'])) {
1306+
$componentInfo[$alias]['agg_field'] = $components['agg_field'];
1307+
}
12921308
if (isset($components['map'])) {
12931309
$componentInfo[$alias]['map'] = $components['map'];
12941310
}

0 commit comments

Comments
 (0)