mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
FIX Ambiguous column SQL error
Specify the table for the field we’re fetching, in case a joined table has a field with the same name
This commit is contained in:
parent
4c7ba731be
commit
b0445f72e4
@ -392,7 +392,8 @@ class DataQuery {
|
||||
* automatically so must not contain double quotes.
|
||||
*/
|
||||
public function max($field) {
|
||||
return $this->aggregate("MAX(\"$field\")");
|
||||
$table = ClassInfo::table_for_object_field($this->dataClass, $field);
|
||||
return $this->aggregate("MAX(\"$table\".\"$field\")");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -402,7 +403,8 @@ class DataQuery {
|
||||
* automatically so must not contain double quotes.
|
||||
*/
|
||||
public function min($field) {
|
||||
return $this->aggregate("MIN(\"$field\")");
|
||||
$table = ClassInfo::table_for_object_field($this->dataClass, $field);
|
||||
return $this->aggregate("MIN(\"$table\".\"$field\")");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -412,7 +414,8 @@ class DataQuery {
|
||||
* automatically so must not contain double quotes.
|
||||
*/
|
||||
public function avg($field) {
|
||||
return $this->aggregate("AVG(\"$field\")");
|
||||
$table = ClassInfo::table_for_object_field($this->dataClass, $field);
|
||||
return $this->aggregate("AVG(\"$table\".\"$field\")");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -422,7 +425,8 @@ class DataQuery {
|
||||
* automatically so must not contain double quotes.
|
||||
*/
|
||||
public function sum($field) {
|
||||
return $this->aggregate("SUM(\"$field\")");
|
||||
$table = ClassInfo::table_for_object_field($this->dataClass, $field);
|
||||
return $this->aggregate("SUM(\"$table\".\"$field\")");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -363,6 +363,17 @@ class DataListTest extends SapphireTest {
|
||||
$this->assertEquals($otherExpected, $otherMap);
|
||||
}
|
||||
|
||||
public function testAmbiguousAggregate() {
|
||||
// Test that we avoid ambiguity error when a field exists on two joined tables
|
||||
// Fetch the sponsors in a round-about way to simulate this
|
||||
$teamID = $this->idFromFixture('DataObjectTest_Team','team2');
|
||||
$sponsors = DataObjectTest_EquipmentCompany::get()->filter('SponsoredTeams.ID', $teamID);
|
||||
$this->assertNotNull($sponsors->Max('ID'));
|
||||
$this->assertNotNull($sponsors->Min('ID'));
|
||||
$this->assertNotNull($sponsors->Avg('ID'));
|
||||
$this->assertNotNull($sponsors->Sum('ID'));
|
||||
}
|
||||
|
||||
public function testEach() {
|
||||
$list = DataObjectTest_TeamComment::get();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user