Skip to content

Commit

Permalink
FIX: DataObject::db returned fields in incorrect order, with incorrec…
Browse files Browse the repository at this point in the history
…t data types
  • Loading branch information
kinglozzer committed Jan 19, 2015
1 parent 9e4c0c6 commit ed07bfa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
51 changes: 36 additions & 15 deletions model/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -1665,38 +1665,59 @@ public function belongs_to($component = null, $classOnly = true) {
* @return array The database fields
*/
public function db($fieldName = null) {
if($fieldName) {
$dataType = $this->dbField($fieldName);
return $dataType ? $dataType : array();

This comment has been minimized.

Copy link
@kinglozzer

kinglozzer Jan 19, 2015

Author Owner

return array() for backwards-compatibility

}

$classes = ClassInfo::ancestry($this, true);
$items = array();

foreach(array_reverse($classes) as $class) {
foreach($classes as $class) {
if(isset(self::$_cache_db[$class])) {
$dbItems = self::$_cache_db[$class];
} else {
$dbItems = (array) Config::inst()->get($class, 'db', Config::UNINHERITED);
self::$_cache_db[$class] = $dbItems;
}

if($fieldName) {
if(isset($dbItems[$fieldName])) {
return $dbItems[$fieldName];
}
} else {
// Validate the data
foreach($dbItems as $k => $v) {
if(!is_string($k) || is_numeric($k) || !is_string($v)) {
user_error("$class::\$db has a bad entry: "
. var_export($k,true). " => " . var_export($v,true) . ". Each map key should be a"
. " property name, and the map value should be the property type.", E_USER_ERROR);
}
// Validate the data
foreach($dbItems as $k => $v) {
if(!is_string($k) || is_numeric($k) || !is_string($v)) {
user_error("$class::\$db has a bad entry: "
. var_export($k,true). " => " . var_export($v,true) . ". Each map key should be a"
. " property name, and the map value should be the property type.", E_USER_ERROR);
}

$items = isset($items) ? array_merge((array) $items, $dbItems) : $dbItems;
}

$items = isset($items) ? array_merge((array) $items, $dbItems) : $dbItems;
}

return $items;
}

/**
* Return the data type for the given database field
* @param string fieldName
* @return string|null
*/
public function dbField($fieldName) {
$classes = ClassInfo::ancestry($this, true);

foreach(array_reverse($classes) as $class) {
if(isset(self::$_cache_db[$class])) {
$dbItems = self::$_cache_db[$class];
} else {
$dbItems = (array) Config::inst()->get($class, 'db', Config::UNINHERITED);
self::$_cache_db[$class] = $dbItems;
}

if(isset($dbItems[$fieldName])) {
return $dbItems[$fieldName];
}
}
}

/**
* Gets the class of a one-to-many relationship. If no $component is specified then an array of all the one-to-many
* relationships and their classes will be returned.
Expand Down
10 changes: 10 additions & 0 deletions tests/model/DataObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,19 @@ public function testDb() {
$this->assertEquals('Text', $obj->db('Comment'));

$obj = new DataObjectTest_ExtendedTeamComment();
$dbFields = $obj->db();

// Assert overloaded fields have correct data type
$this->assertEquals('HTMLText', $obj->db('Comment'));
$this->assertEquals('HTMLText', $dbFields['Comment'],
'Calls to DataObject::db without a field specified return correct data types');

// assertEquals doesn't verify the order of array elements, so access keys manually to check order:
// expected: array('Name' => 'Varchar', 'Comment' => 'HTMLText')
reset($dbFields);
$this->assertEquals('Name', key($dbFields), 'DataObject::db returns fields in correct order');
next($dbFields);
$this->assertEquals('Comment', key($dbFields), 'DataObject::db returns fields in correct order');
}

public function testValidObjectsForBaseFields() {
Expand Down

0 comments on commit ed07bfa

Please sign in to comment.