mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #1861 from raket/dataobject-update-orphan
Fix: Orphaned records when running DataObject::update
This commit is contained in:
commit
c26ddee191
@ -784,10 +784,15 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
// no support for has_many or many_many relationships,
|
||||
// as the updater wouldn't know which object to write to (or create)
|
||||
if($relObj->$relation() instanceof DataObject) {
|
||||
$parentObj = $relObj;
|
||||
$relObj = $relObj->$relation();
|
||||
|
||||
// If the intermediate relationship objects have been created, then write them
|
||||
if($i<sizeof($relation)-1 && !$relObj->ID) $relObj->write();
|
||||
if($i<sizeof($relation)-1 && !$relObj->ID || (!$relObj->ID && $parentObj != $this)) {
|
||||
$relObj->write();
|
||||
$relatedFieldName = $relation."ID";
|
||||
$parentObj->$relatedFieldName = $relObj->ID;
|
||||
$parentObj->write();
|
||||
}
|
||||
} else {
|
||||
user_error(
|
||||
"DataObject::update(): Can't traverse relationship '$relation'," .
|
||||
@ -803,6 +808,8 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
if($relObj) {
|
||||
$relObj->$fieldName = $v;
|
||||
$relObj->write();
|
||||
$relatedFieldName = $relation."ID";
|
||||
$this->$relatedFieldName = $relObj->ID;
|
||||
$relObj->flushCache();
|
||||
} else {
|
||||
user_error("Couldn't follow dot syntax '$k' on '$this->class' object", E_USER_WARNING);
|
||||
|
@ -642,6 +642,29 @@ class DataObjectTest extends SapphireTest {
|
||||
$this->assertEquals('New and improved team 1', $reloadedTeam1->Title);
|
||||
}
|
||||
|
||||
public function testDataObjectUpdateNew() {
|
||||
/* update() calls can use the dot syntax to reference has_one relations and other methods that return
|
||||
* objects */
|
||||
$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
||||
$team1->CaptainID = 0;
|
||||
|
||||
$team1->update(array(
|
||||
'Captain.FirstName' => 'Jim',
|
||||
'Captain.FavouriteTeam.Title' => 'New and improved team 1',
|
||||
));
|
||||
/* Test that the captain ID has been updated */
|
||||
$this->assertGreaterThan(0, $team1->CaptainID);
|
||||
|
||||
/* Fetch the newly created captain */
|
||||
$captain1 = DataObjectTest_Player::get()->byID($team1->CaptainID);
|
||||
$this->assertEquals('Jim', $captain1->FirstName);
|
||||
|
||||
/* Grab the favourite team and make sure it has the correct values */
|
||||
$reloadedTeam1 = $captain1->FavouriteTeam();
|
||||
$this->assertEquals($reloadedTeam1->ID, $captain1->FavouriteTeamID);
|
||||
$this->assertEquals('New and improved team 1', $reloadedTeam1->Title);
|
||||
}
|
||||
|
||||
public function testWritingInvalidDataObjectThrowsException() {
|
||||
$validatedObject = new DataObjectTest_ValidatedObject();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user