Merge branch '3' into 4

This commit is contained in:
Daniel Hensby 2017-08-17 15:02:25 +01:00
commit 33c2c7bfe7
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E
2 changed files with 31 additions and 6 deletions

View File

@ -1349,6 +1349,12 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
// Check changes exist, abort if there are none
$hasChanges = $this->updateChanges($isNewRecord);
if ($hasChanges || $forceWrite || $isNewRecord) {
// Ensure Created and LastEdited are populated
if (!isset($this->record['Created'])) {
$this->record['Created'] = $now;
}
$this->record['LastEdited'] = $now;
// New records have their insert into the base data table done first, so that they can pass the
// generated primary key on to the rest of the manipulation
$baseTable = $this->baseTable();
@ -1370,12 +1376,6 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$this->invokeWithExtensions('onAfterSkippedWrite');
}
// Ensure Created and LastEdited are populated
if (!isset($this->record['Created'])) {
$this->record['Created'] = $now;
}
$this->record['LastEdited'] = $now;
// Write relations as necessary
if ($writeComponents) {
$this->writeComponents(true);

View File

@ -7,6 +7,7 @@ use SilverStripe\Dev\SapphireTest;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\DataObjectSchema;
use SilverStripe\ORM\FieldType\DBBoolean;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
@ -14,6 +15,7 @@ use SilverStripe\ORM\Connect\MySQLDatabase;
use SilverStripe\ORM\FieldType\DBPolymorphicForeignKey;
use SilverStripe\ORM\FieldType\DBVarchar;
use SilverStripe\ORM\ManyManyList;
use SilverStripe\ORM\Tests\DataObjectTest\Player;
use SilverStripe\ORM\ValidationException;
use SilverStripe\View\ViewableData;
use stdClass;
@ -449,6 +451,29 @@ class DataObjectTest extends SapphireTest
$this->assertEquals(1, $players->limit(5, 3)->count());
}
public function testWriteNoChangesDoesntUpdateLastEdited()
{
// set mock now so we can be certain of LastEdited time for our test
DBDatetime::set_mock_now('2017-01-01 00:00:00');
$obj = new Player();
$obj->FirstName = 'Test';
$obj->Surname = 'Plater';
$obj->Email = 'test.player@example.com';
$obj->write();
$this->assertEquals('2017-01-01 00:00:00', $obj->LastEdited);
$writtenObj = Player::get()->byID($obj->ID);
$this->assertEquals('2017-01-01 00:00:00', $writtenObj->LastEdited);
// set mock now so we get a new LastEdited if, for some reason, it's updated
DBDatetime::set_mock_now('2017-02-01 00:00:00');
$writtenObj->write();
$this->assertEquals('2017-01-01 00:00:00', $writtenObj->LastEdited);
$this->assertEquals($obj->ID, $writtenObj->ID);
$reWrittenObj = Player::get()->byID($writtenObj->ID);
$this->assertEquals('2017-01-01 00:00:00', $reWrittenObj->LastEdited);
}
/**
* Test writing of database columns which don't correlate to a DBField,
* e.g. all relation fields on has_one/has_many like "ParentID".