From 712680a5e8e7fa2b5e5000be8a36fd02df66c40c Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Wed, 13 Oct 2010 03:58:10 +0000 Subject: [PATCH] BUGFIX #5362: Fixed duplicate removal on DataObject:get() with join argument for all databases. (from r103588) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112150 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/DataObject.php | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/core/model/DataObject.php b/core/model/DataObject.php index 2df7ce62f..a457b5e8d 100755 --- a/core/model/DataObject.php +++ b/core/model/DataObject.php @@ -1470,7 +1470,10 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity $limit, "INNER JOIN \"$table\" ON \"$table\".\"$componentField\" = \"$componentBaseClass\".\"ID\"" // join ); - array_unshift($query->select, "\"$table\".*"); + + foreach((array)$this->many_many_extraFields($componentName) as $extraField => $extraFieldType) { + $query->select[] = "\"$table\".\"$extraField\""; + } if($filter) $query->where[] = $filter; if($join) $query->from[] = $join; @@ -1687,15 +1690,19 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity $candidateManyMany = $SNG_candidate->stat('belongs_many_many'); // Find the relation given the class + $relationName = null; if($candidateManyMany) foreach($candidateManyMany as $relation => $relatedClass) { if($relatedClass == $class) { $relationName = $relation; + break; } } - $extraFields = $SNG_candidate->stat('many_many_extraFields'); - if(isset($extraFields[$relationName])) { - return $extraFields[$relationName]; + if($relationName) { + $extraFields = $SNG_candidate->stat('many_many_extraFields'); + if(isset($extraFields[$relationName])) { + return $extraFields[$relationName]; + } } } @@ -2640,13 +2647,17 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity if($join) { $query->from[] = $join; - if(DB::getConn() instanceof MySQLDatabase || DB::getConn() instanceof SQLite3Database) { - // TODO: This needs to be resolved for all databases - $query->groupby[] = reset($query->from) . ".\"ID\""; - /* this needs to be fixed - this doesn't work when you add additional fields from other tables into the mix. - $fields = $this->databaseFields(); - foreach(array_keys($fields) as $field) $query->groupby[] = "\"$field\""; - */ + // In order to group by unique columns we have to group by everything listed in the select + foreach($query->select as $field) { + // Identify columns with aliases, and ignore the alias. Making use of the alias in + // group by was causing problems when those queries were subsequently passed into + // SQLQuery::unlimitedRowCount. + if(preg_match('/^(.*)\s+AS\s+(\"[^"]+\")\s*$/', $field, $matches)) { + $query->groupby[] = $matches[1]; + // Otherwise just use the field as is + } else { + $query->groupby[] = $field; + } } }