mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #4215 from russellshome/patch-1
* russellshome-patch-1: DataObject accept arrays or stdClass
This commit is contained in:
commit
599c054d4f
@ -400,6 +400,10 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
$record = null;
|
||||
}
|
||||
|
||||
if(is_a($record, "stdClass")) {
|
||||
$record = (array)$record;
|
||||
}
|
||||
|
||||
// Set $this->record to $record, but ignore NULLs
|
||||
$this->record = array();
|
||||
foreach($record as $k => $v) {
|
||||
|
@ -53,6 +53,30 @@ class DataObjectTest extends SapphireTest {
|
||||
$this->assertEquals('Comment', key($dbFields), 'DataObject::db returns fields in correct order');
|
||||
}
|
||||
|
||||
public function testConstructAcceptsValues() {
|
||||
// Values can be an array...
|
||||
$player = new DataObjectTest_Player(array(
|
||||
'FirstName' => 'James',
|
||||
'Surname' => 'Smith'
|
||||
));
|
||||
|
||||
$this->assertEquals('James', $player->FirstName);
|
||||
$this->assertEquals('Smith', $player->Surname);
|
||||
|
||||
// ... or a stdClass inst
|
||||
$data = new stdClass();
|
||||
$data->FirstName = 'John';
|
||||
$data->Surname = 'Doe';
|
||||
$player = new DataObjectTest_Player($data);
|
||||
|
||||
$this->assertEquals('John', $player->FirstName);
|
||||
$this->assertEquals('Doe', $player->Surname);
|
||||
|
||||
// IDs should be stored as integers, not strings
|
||||
$player = new DataObjectTest_Player(array('ID' => '5'));
|
||||
$this->assertSame(5, $player->ID);
|
||||
}
|
||||
|
||||
public function testValidObjectsForBaseFields() {
|
||||
$obj = new DataObjectTest_ValidatedObject();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user