From ebae480c662032d58a14f44055428b9309563874 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Thu, 10 Nov 2016 11:13:02 +1300 Subject: [PATCH] BUG Fix regression in aggregate column lookup from #6199 --- core/ClassInfo.php | 3 +++ model/DataQuery.php | 16 ++++++++++++++++ tests/model/DataListTest.php | 7 +++++++ 3 files changed, 26 insertions(+) diff --git a/core/ClassInfo.php b/core/ClassInfo.php index a4339c40f..fdd1e18c4 100644 --- a/core/ClassInfo.php +++ b/core/ClassInfo.php @@ -298,6 +298,9 @@ class ClassInfo { * field column for a {@link DataObject}. If the field does not exist, this * will return null. * + * Note: In 3.x and below this method may return 'DataObject'. From 4.0 onwards + * null will be returned if a field is not a member of the object. + * * @param string $candidateClass * @param string $fieldName * diff --git a/model/DataQuery.php b/model/DataQuery.php index cf8fe839b..9ccb5912b 100644 --- a/model/DataQuery.php +++ b/model/DataQuery.php @@ -390,9 +390,13 @@ class DataQuery { * * @param String $field Unquoted database column name. Will be ANSI quoted * automatically so must not contain double quotes. + * @return string */ public function max($field) { $table = ClassInfo::table_for_object_field($this->dataClass, $field); + if (!$table || $table === 'DataObject') { + return $this->aggregate("MAX(\"$field\")"); + } return $this->aggregate("MAX(\"$table\".\"$field\")"); } @@ -401,9 +405,13 @@ class DataQuery { * * @param String $field Unquoted database column name. Will be ANSI quoted * automatically so must not contain double quotes. + * @return string */ public function min($field) { $table = ClassInfo::table_for_object_field($this->dataClass, $field); + if (!$table || $table === 'DataObject') { + return $this->aggregate("MIN(\"$field\")"); + } return $this->aggregate("MIN(\"$table\".\"$field\")"); } @@ -412,9 +420,13 @@ class DataQuery { * * @param String $field Unquoted database column name. Will be ANSI quoted * automatically so must not contain double quotes. + * @return string */ public function avg($field) { $table = ClassInfo::table_for_object_field($this->dataClass, $field); + if (!$table || $table === 'DataObject') { + return $this->aggregate("AVG(\"$field\")"); + } return $this->aggregate("AVG(\"$table\".\"$field\")"); } @@ -423,9 +435,13 @@ class DataQuery { * * @param String $field Unquoted database column name. Will be ANSI quoted * automatically so must not contain double quotes. + * @return string */ public function sum($field) { $table = ClassInfo::table_for_object_field($this->dataClass, $field); + if (!$table || $table === 'DataObject') { + return $this->aggregate("SUM(\"$field\")"); + } return $this->aggregate("SUM(\"$table\".\"$field\")"); } diff --git a/tests/model/DataListTest.php b/tests/model/DataListTest.php index 21299c88e..ffcc94d63 100755 --- a/tests/model/DataListTest.php +++ b/tests/model/DataListTest.php @@ -372,6 +372,13 @@ class DataListTest extends SapphireTest { $this->assertNotNull($sponsors->Min('ID')); $this->assertNotNull($sponsors->Avg('ID')); $this->assertNotNull($sponsors->Sum('ID')); + + // Test non-orm many_many_extraFields + $company = $this->objFromFixture('DataObjectTest_EquipmentCompany', 'equipmentcompany1'); + $this->assertNotNull($company->SponsoredTeams()->Max('SponsorFee')); + $this->assertNotNull($company->SponsoredTeams()->Min('SponsorFee')); + $this->assertNotNull($company->SponsoredTeams()->Avg('SponsorFee')); + $this->assertNotNull($company->SponsoredTeams()->Sum('SponsorFee')); } public function testEach() {