mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
6f9aa32ccc
commit
e86cf93f35
@ -2599,10 +2599,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 {
|
||||
|
@ -739,6 +739,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");
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user