mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
BUG Fix regression in has_one getters breaking DataDifferencer
This commit is contained in:
parent
80072f01a7
commit
db9aa2c5c7
@ -2370,8 +2370,8 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
|||||||
*/
|
*/
|
||||||
public function getField($field)
|
public function getField($field)
|
||||||
{
|
{
|
||||||
// If we already have an object in $this->record, then we should just return that
|
// If we already have a value in $this->record, then we should just return that
|
||||||
if (isset($this->record[$field]) && is_object($this->record[$field])) {
|
if (isset($this->record[$field])) {
|
||||||
return $this->record[$field];
|
return $this->record[$field];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2591,14 +2591,16 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
|||||||
// Support component assignent via field setter
|
// Support component assignent via field setter
|
||||||
$schema = static::getSchema();
|
$schema = static::getSchema();
|
||||||
if ($schema->unaryComponent(static::class, $fieldName)) {
|
if ($schema->unaryComponent(static::class, $fieldName)) {
|
||||||
|
unset($this->components[$fieldName]);
|
||||||
// Assign component directly
|
// Assign component directly
|
||||||
if (is_null($val) || $val instanceof DataObject) {
|
if (is_null($val) || $val instanceof DataObject) {
|
||||||
return $this->setComponent($fieldName, $val);
|
return $this->setComponent($fieldName, $val);
|
||||||
}
|
}
|
||||||
// Assign by ID instead of object
|
// Assign by ID instead of object
|
||||||
unset($this->components[$fieldName]);
|
if (is_numeric($val)) {
|
||||||
$fieldName .= 'ID';
|
$fieldName .= 'ID';
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Situation 1: Passing an DBField
|
// Situation 1: Passing an DBField
|
||||||
if ($val instanceof DBField) {
|
if ($val instanceof DBField) {
|
||||||
|
@ -673,6 +673,31 @@ class DataObjectTest extends SapphireTest
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test has_one used as field getter/setter
|
||||||
|
*/
|
||||||
|
public function testHasOneAsField()
|
||||||
|
{
|
||||||
|
/** @var DataObjectTest\Team $team1 */
|
||||||
|
$team1 = $this->objFromFixture(DataObjectTest\Team::class, 'team1');
|
||||||
|
$captain1 = $this->objFromFixture(DataObjectTest\Player::class, 'captain1');
|
||||||
|
$captain2 = $this->objFromFixture(DataObjectTest\Player::class, 'captain2');
|
||||||
|
|
||||||
|
// Setter: By RelationID
|
||||||
|
$team1->CaptainID = $captain1->ID;
|
||||||
|
$team1->write();
|
||||||
|
$this->assertEquals($captain1->ID, $team1->Captain->ID);
|
||||||
|
|
||||||
|
// Setter: New object
|
||||||
|
$team1->Captain = $captain2;
|
||||||
|
$team1->write();
|
||||||
|
$this->assertEquals($captain2->ID, $team1->Captain->ID);
|
||||||
|
|
||||||
|
// Setter: Custom data (required by DataDifferencer)
|
||||||
|
$team1->Captain = DBField::create_field('HTMLFragment', '<p>No captain</p>');
|
||||||
|
$this->assertEquals('<p>No captain</p>', $team1->Captain);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @todo Extend type change tests (e.g. '0'==NULL)
|
* @todo Extend type change tests (e.g. '0'==NULL)
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user