diff --git a/core/model/DataObject.php b/core/model/DataObject.php index 910867a72..6de7a483d 100644 --- a/core/model/DataObject.php +++ b/core/model/DataObject.php @@ -713,8 +713,13 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity * if they are not already marked as changed. */ public function forceChange() { - foreach($this->record as $fieldName => $fieldVal) { + // $this->record might not contain the blank values so we loop on $this->inheritedDatabaseFields() as well + $fieldNames = array_unique(array_merge(array_keys($this->record), array_keys($this->inheritedDatabaseFields()))); + + foreach($fieldNames as $fieldName) { if(!isset($this->changed[$fieldName])) $this->changed[$fieldName] = 1; + // Populate the null values in record so that they actually get written + if(!isset($this->record[$fieldName])) $this->record[$fieldName] = null; } // @todo Find better way to allow versioned to write a new version after forceChange diff --git a/tests/SiteTreeTest.php b/tests/SiteTreeTest.php index 1d598e6eb..4693136ee 100644 --- a/tests/SiteTreeTest.php +++ b/tests/SiteTreeTest.php @@ -45,6 +45,25 @@ class SiteTreeTest extends SapphireTest { $this->assertEquals($obj->ID, $createdID); } + /** + * Test that field which are set and then cleared are also transferred to the published site. + */ + function testPublishDeletedFields() { + $obj = $this->fixture->objFromFixture('Page', 'about'); + $obj->MetaTitle = "asdfasdf"; + $obj->write(); + $obj->doPublish(); + + $this->assertEquals('asdfasdf', DB::query("SELECT \"MetaTitle\" FROM \"SiteTree_Live\" WHERE \"ID\" = '$obj->ID'")->value()); + + $obj->MetaTitle = null; + $obj->write(); + $obj->doPublish(); + + $this->assertNull(DB::query("SELECT \"MetaTitle\" FROM \"SiteTree_Live\" WHERE \"ID\" = '$obj->ID'")->value()); + + } + function testParentNodeCachedInMemory() { $parent = new SiteTree(); $parent->Title = 'Section Title';