Merge pull request #4725 from tractorcow/pulls/3.2/fix-id-filter

BUG Ensure that filters on any fixed field are scoped to the base data table
This commit is contained in:
Sam Minnée 2015-10-30 17:00:31 +00:00
commit 77b084c324
3 changed files with 26 additions and 7 deletions

View File

@ -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);
if($inst->hasOwnTableDatabaseField($fieldName)) {
break;
// Short circuit for fixed fields
$fixed = DataObject::config()->fixed_fields;
if($exists && isset($fixed[$fieldName])) {
return self::baseDataClass($candidateClass);
}
// 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);

View File

@ -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')
);
}
}

View File

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