From 2813f94124c2ba14f1e4a51001e3898b0e0c32aa Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Fri, 30 Oct 2015 16:26:14 +1300 Subject: [PATCH] BUG Ensure that filters on any fixed field are scoped to the base data table Fixes #4700 --- core/ClassInfo.php | 18 +++++++++++------- tests/core/ClassInfoTest.php | 10 ++++++++++ tests/model/DataListTest.php | 5 +++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/core/ClassInfo.php b/core/ClassInfo.php index af166f77a..5d7672d3f 100644 --- a/core/ClassInfo.php +++ b/core/ClassInfo.php @@ -307,16 +307,20 @@ class ClassInfo { //normalise class name $candidateClass = self::class_name($candidateClass); - $exists = self::exists($candidateClass); - while($candidateClass && $candidateClass != 'DataObject' && $exists) { - if(DataObject::has_own_table($candidateClass)) { - $inst = singleton($candidateClass); + // Short circuit for fixed fields + $fixed = DataObject::config()->fixed_fields; + if($exists && isset($fixed[$fieldName])) { + return self::baseDataClass($candidateClass); + } - if($inst->hasOwnTableDatabaseField($fieldName)) { - break; - } + // Find regular field + while($candidateClass && $candidateClass != 'DataObject' && $exists) { + if( DataObject::has_own_table($candidateClass) + && DataObject::has_own_table_database_field($candidateClass, $fieldName) + ) { + break; } $candidateClass = get_parent_class($candidateClass); diff --git a/tests/core/ClassInfoTest.php b/tests/core/ClassInfoTest.php index 923564fcc..fd437f41e 100644 --- a/tests/core/ClassInfoTest.php +++ b/tests/core/ClassInfoTest.php @@ -197,6 +197,16 @@ class ClassInfoTest extends SapphireTest { $this->assertNull( ClassInfo::table_for_object_field(null, null) ); + + // Test fixed fields + $this->assertEquals( + 'ClassInfoTest_BaseDataClass', + ClassInfo::table_for_object_field('ClassInfoTest_HasFields', 'ID') + ); + $this->assertEquals( + 'ClassInfoTest_BaseDataClass', + ClassInfo::table_for_object_field('ClassInfoTest_NoFields', 'Created') + ); } } diff --git a/tests/model/DataListTest.php b/tests/model/DataListTest.php index b894daebc..7d7b453bc 100755 --- a/tests/model/DataListTest.php +++ b/tests/model/DataListTest.php @@ -400,6 +400,11 @@ class DataListTest extends SapphireTest { // byID() returns a DataObject, rather than a DataList $this->assertInstanceOf('DataObjectTest_Team', $team); $this->assertEquals('Team 2', $team->Title); + + // Assert that filtering on ID searches by the base table, not the child table field + $query = DataObjectTest_SubTeam::get()->filter('ID', 4)->sql($parameters); + $this->assertContains('WHERE ("DataObjectTest_Team"."ID" = ?)', $query); + $this->assertNotContains('WHERE ("DataObjectTest_SubTeam"."ID" = ?)', $query); } /**