Merge pull request #6351 from steve-silverstripe/patch-2

This commit is contained in:
Daniel Hensby 2017-03-03 12:39:31 +00:00
commit 7f194a917d
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E
2 changed files with 25 additions and 2 deletions

View File

@ -594,7 +594,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider {
// Set up a new entry in (table)_versions
$newManipulation = array(
"command" => "insert",
"fields" => isset($manipulation[$table]['fields']) ? $manipulation[$table]['fields'] : null
"fields" => isset($manipulation[$table]['fields']) ? $manipulation[$table]['fields'] : array(),
);
// Add any extra, unchanged fields to the version record.
@ -607,7 +607,8 @@ class Versioned extends DataExtension implements TemplateGlobalProvider {
$data = array_intersect_key($data, $fields);
foreach ($data as $k => $v) {
if (!isset($newManipulation['fields'][$k])) {
// If the value is not set at all in the manipulation currently, use the existing value from the database
if (!array_key_exists($k, $newManipulation['fields'])) {
$newManipulation['fields'][$k] = $v;
}
}

View File

@ -896,6 +896,28 @@ class VersionedTest extends SapphireTest {
$this->assertTrue($public->canViewStage('Live'));
$this->assertTrue($private->canViewStage('Live'));
}
/**
* Values that are overwritten with null are saved to the _versions table correctly.
*/
public function testWriteNullValueToVersion() {
$record = VersionedTest_Subclass::create();
$record->Title = "Test A";
$record->write();
$version = Versioned::get_latest_version($record->ClassName, $record->ID);
$this->assertEquals(1, $version->Version);
$this->assertEquals($record->Title, $version->Title);
$record->Title = null;
$record->write();
$version = Versioned::get_latest_version($record->ClassName, $record->ID);
$this->assertEquals(2, $version->Version);
$this->assertEquals($record->Title, $version->Title);
}
}