Skip to content

Commit

Permalink
BUGFIX #5362: Fixed duplicate removal on DataObject:get() with join a…
Browse files Browse the repository at this point in the history
…rgument for all databases.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@103588 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information
Sam Minnee committed Feb 2, 2011
1 parent d12183b commit 6a72c21
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions core/model/DataObject.php
Expand Up @@ -1419,7 +1419,10 @@ public function getManyManyComponentsQuery($componentName, $filter = "", $sort =
$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;
Expand Down Expand Up @@ -1636,15 +1639,19 @@ public function many_many_extraFields($component = null) {
$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];
}
}
}

Expand Down Expand Up @@ -2589,13 +2596,17 @@ public function buildSQL($filter = "", $sort = "", $limit = "", $join = "", $res

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;
}
}
}

Expand Down

0 comments on commit 6a72c21

Please sign in to comment.