Merge pull request #6199 from jonom/fix-ambiguous-field

FIX Ambiguous column SQL error
This commit is contained in:
Damian Mooyman 2016-10-19 17:57:32 +13:00 committed by GitHub
commit 0f8f9f2732
2 changed files with 19 additions and 4 deletions

View File

@ -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\")");
}
/**

View File

@ -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();