FIX: DataObject::db() doesn't respect overloaded db types (fixes #3620)

This commit is contained in:
Loz Calver 2014-11-10 21:12:25 +00:00
parent ca9c192c65
commit 85b4ba15fc
2 changed files with 25 additions and 15 deletions

View File

@ -1664,19 +1664,10 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* @return array The database fields
*/
public function db($fieldName = null) {
$classes = ClassInfo::ancestry($this);
$good = false;
$classes = ClassInfo::ancestry($this, true);
$items = array();
foreach($classes as $class) {
// Wait until after we reach DataObject
if(!$good) {
if($class == 'DataObject') {
$good = true;
}
continue;
}
foreach(array_reverse($classes) as $class) {
if(isset(self::$_cache_db[$class])) {
$dbItems = self::$_cache_db[$class];
} else {

View File

@ -16,18 +16,31 @@ class DataObjectTest extends SapphireTest {
'DataObjectTest_FieldlessSubTable',
'DataObjectTest_ValidatedObject',
'DataObjectTest_Player',
'DataObjectTest_TeamComment'
'DataObjectTest_TeamComment',
'DataObjectTest_ExtendedTeamComment'
);
public function testBaseFieldsExcludedFromDb() {
$obj = new DataObjectTest_ValidatedObject();
public function testDb() {
$obj = new DataObjectTest_TeamComment();
$dbFields = $obj->db();
// Assert fields are included
$this->assertArrayHasKey('Name', $dbFields);
// Assert the base fields are excluded
$this->assertArrayNotHasKey('Created', $dbFields);
$this->assertArrayNotHasKey('LastEdited', $dbFields);
$this->assertArrayNotHasKey('ClassName', $dbFields);
$this->assertArrayNotHasKey('ID', $dbFields);
// Assert that the correct field type is returned when passing a field
$this->assertEquals('Varchar', $obj->db('Name'));
$this->assertEquals('Text', $obj->db('Comment'));
$obj = new DataObjectTest_ExtendedTeamComment();
// Assert overloaded fields have correct data type
$this->assertEquals('HTMLText', $obj->db('Comment'));
}
public function testValidObjectsForBaseFields() {
@ -1443,5 +1456,11 @@ class DataObjectTest_TeamComment extends DataObject {
}
class DataObjectTest_ExtendedTeamComment extends DataObjectTest_TeamComment {
private static $db = array(
'Comment' => 'HTMLText'
);
}
DataObjectTest_Team::add_extension('DataObjectTest_Team_Extension');