mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
commit
77b084c324
@ -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);
|
||||
|
@ -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')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user