Merge pull request #921 from TheFrozenFire/bug-DataObjectCreated

BUG: DataObject::write overwrites Created on first write
This commit is contained in:
Simon Welsh 2012-11-03 02:08:35 -07:00
commit 5a793990d8
2 changed files with 25 additions and 2 deletions

View File

@ -1147,7 +1147,13 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
if($idx == 0) {
$manipulation[$class]['fields']["LastEdited"] = "'".SS_Datetime::now()->Rfc2822()."'";
if($dbCommand == 'insert') {
$manipulation[$class]['fields']["Created"] = "'".SS_Datetime::now()->Rfc2822()."'";
if(!empty($this->record["Created"])) {
$manipulation[$class]['fields']["Created"]
= DB::getConn()->prepStringForDB($this->record["Created"]);
} else {
$manipulation[$class]['fields']["Created"]
= DB::getConn()->prepStringForDB(SS_Datetime::now()->Rfc2822());
}
//echo "<li>$this->class - " .get_class($this);
$manipulation[$class]['fields']["ClassName"]
= DB::getConn()->prepStringForDB($this->class);

View File

@ -1127,7 +1127,24 @@ class DataObjectTest extends SapphireTest {
}
public function testWriteOverwritesCreated() {
// Previously, if you set DataObject::$Created before the object was first written, it would be overwritten
$pastdate = new SS_DateTime();
$pastdate->setValue(Date::past_date(1));
$obj = new DataObjectTest_Player();
$obj->Created = $pastdate->Value;
$obj->write();
$objID = $obj->ID;
unset($obj);
DataObject::reset();
$obj = DataObjectTest_Player::get()->byID($objID);
$this->assertEquals($pastdate->Value, $obj->Created);
}
}
class DataObjectTest_Player extends Member implements TestOnly {