BUG Ensure that filters on any fixed field are scoped to the base data table

Fixes #4700
This commit is contained in:
Damian Mooyman 2015-10-30 16:26:14 +13:00
parent 9c4443629e
commit 2813f94124
3 changed files with 26 additions and 7 deletions

View File

@ -307,16 +307,20 @@ class ClassInfo {
//normalise class name //normalise class name
$candidateClass = self::class_name($candidateClass); $candidateClass = self::class_name($candidateClass);
$exists = self::exists($candidateClass); $exists = self::exists($candidateClass);
while($candidateClass && $candidateClass != 'DataObject' && $exists) { // Short circuit for fixed fields
if(DataObject::has_own_table($candidateClass)) { $fixed = DataObject::config()->fixed_fields;
$inst = singleton($candidateClass); if($exists && isset($fixed[$fieldName])) {
return self::baseDataClass($candidateClass);
}
if($inst->hasOwnTableDatabaseField($fieldName)) { // Find regular field
break; 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); $candidateClass = get_parent_class($candidateClass);

View File

@ -197,6 +197,16 @@ class ClassInfoTest extends SapphireTest {
$this->assertNull( $this->assertNull(
ClassInfo::table_for_object_field(null, null) 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')
);
} }
} }

View File

@ -400,6 +400,11 @@ class DataListTest extends SapphireTest {
// byID() returns a DataObject, rather than a DataList // byID() returns a DataObject, rather than a DataList
$this->assertInstanceOf('DataObjectTest_Team', $team); $this->assertInstanceOf('DataObjectTest_Team', $team);
$this->assertEquals('Team 2', $team->Title); $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);
} }
/** /**