From 9f7bbc57d06d8912439116f59b510a8508e2162e Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sat, 23 May 2009 05:32:16 +0000 Subject: [PATCH] BUGFIX Inspecting the change status of all CompositeDBFields when invoking DataObject->getChangedFields(), as their value setting is not performed through DataObject->setField(), it doesn't trigger the built-in change detection. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@77667 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/DataObject.php | 7 +++++++ tests/model/MoneyTest.php | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/core/model/DataObject.php b/core/model/DataObject.php index b28248bc4..8b66f9c25 100644 --- a/core/model/DataObject.php +++ b/core/model/DataObject.php @@ -1844,6 +1844,13 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity public function getChangedFields($databaseFieldsOnly = false, $changeLevel = 1) { $changedFields = array(); + // Update the changed array with references to changed obj-fields + foreach($this->record as $k => $v) { + if(is_object($v) && method_exists($v, 'isChanged') && $v->isChanged()) { + $this->changed[$k] = true; + } + } + if($databaseFieldsOnly) { $customDatabaseFields = $this->customDatabaseFields(); $fields = array_intersect_key($this->changed, $customDatabaseFields); diff --git a/tests/model/MoneyTest.php b/tests/model/MoneyTest.php index 0a5ba28a7..e2d87c9e0 100644 --- a/tests/model/MoneyTest.php +++ b/tests/model/MoneyTest.php @@ -23,6 +23,13 @@ class MoneyTest extends SapphireTest { $this->assertEquals($obj->MyMoney->getAmount(), 1.23); } + function testDataObjectChangedFields() { + $obj = $this->objFromFixture('MoneyTest_DataObject', 'test1'); + $obj->MyMoney->setAmount(99); + $changed = $obj->getChangedFields(); + $this->assertContains('MyMoney', array_keys($changed)); + } + function testCanOverwriteSettersWithNull() { $obj = new MoneyTest_DataObject();