BUGFIX: Fixed ordering by aggregate columns for DataObject::get() calls with joins.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@103620 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-04-28 01:54:35 +00:00
parent 6f9aa32ccc
commit e86cf93f35
2 changed files with 8 additions and 1 deletions

View File

@ -2599,10 +2599,13 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$query->from[] = $join; $query->from[] = $join;
// In order to group by unique columns we have to group by everything listed in the select // In order to group by unique columns we have to group by everything listed in the select
foreach($query->select as $field) { foreach($query->select as $field) {
// Skip the _SortColumns; these are only going to be aggregate functions
if(preg_match('/AS\s+\"?_SortColumn/', $field, $matches)) {
// Identify columns with aliases, and ignore the alias. Making use of the alias in // 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 // group by was causing problems when those queries were subsequently passed into
// SQLQuery::unlimitedRowCount. // SQLQuery::unlimitedRowCount.
if(preg_match('/^(.*)\s+AS\s+(\"[^"]+\")\s*$/', $field, $matches)) { } else if(preg_match('/^(.*)\s+AS\s+(\"[^"]+\")\s*$/', $field, $matches)) {
$query->groupby[] = $matches[1]; $query->groupby[] = $matches[1];
// Otherwise just use the field as is // Otherwise just use the field as is
} else { } else {

View File

@ -739,6 +739,10 @@ class DataObjectTest extends SapphireTest {
$player = $newTeam->Players()->First(); $player = $newTeam->Players()->First();
$this->assertEquals('Sam', $player->FirstName); $this->assertEquals('Sam', $player->FirstName);
$this->assertEquals("Prop", $player->Position); $this->assertEquals("Prop", $player->Position);
// Check that ordering a many-many relation by an aggregate column doesn't fail
$player = $this->objFromFixture('DataObjectTest_Player', 'player2');
$player->Teams("", "count(DISTINCT \"DataObjectTest_Team_Players\".\"DataObjectTest_PlayerID\") DESC");
} }
/** /**