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

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112155 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-10-13 04:00:52 +00:00
parent 2c4b07ace7
commit 8da5de8d41
2 changed files with 8 additions and 1 deletions

View File

@ -2650,10 +2650,13 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$query->from[] = $join;
// In order to group by unique columns we have to group by everything listed in the select
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
// group by was causing problems when those queries were subsequently passed into
// 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];
// Otherwise just use the field as is
} else {

View File

@ -738,6 +738,10 @@ class DataObjectTest extends SapphireTest {
$player = $newTeam->Players()->First();
$this->assertEquals('Sam', $player->FirstName);
$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");
}
/**