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
This commit is contained in:
Sam Minnee 2010-10-13 03:58:10 +00:00
parent 1e925a9e91
commit 712680a5e8

View File

@ -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,17 +1690,21 @@ 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;
}
}
if($relationName) {
$extraFields = $SNG_candidate->stat('many_many_extraFields');
if(isset($extraFields[$relationName])) {
return $extraFields[$relationName];
}
}
}
$manyMany = $SNG_class->stat('belongs_many_many');
$candidate = (isset($manyMany[$component])) ? $manyMany[$component] : null;
@ -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;
}
}
}