BUGFIX Remove dummy entry created by Versioned if record is first written to Live stage (fixes #5596, thanks muzdowski) (from r107537)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112603 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-10-15 03:53:14 +00:00
parent 577e82a123
commit b64015e2cf
2 changed files with 47 additions and 0 deletions

View File

@ -396,6 +396,11 @@ class Versioned extends DataObjectDecorator {
// If we're editing Live, then use (table)_Live instead of (table)
if(Versioned::current_stage() && Versioned::current_stage() != $this->defaultStage) {
// If the record has already been inserted in the (table), get rid of it.
if($manipulation[$table]['command']=='insert') {
DB::query("DELETE FROM \"{$table}\" WHERE \"ID\"='$id'");
}
$newTable = $table . '_' . Versioned::current_stage();
$manipulation[$newTable] = $manipulation[$table];
unset($manipulation[$table]);

View File

@ -117,6 +117,48 @@ class VersionedTest extends SapphireTest {
$this->assertEquals(0, DB::query('SELECT COUNT(*) FROM "SiteTree_Live" WHERE "ID" = '.$pageID)->value());
}
function testWritingNewToStage() {
$origStage = Versioned::current_stage();
Versioned::reading_stage("Stage");
$page = new Page();
$page->Title = "testWritingNewToStage";
$page->URLSegment = "testWritingNewToStage";
$page->write();
$live = Versioned::get_by_stage('SiteTree', 'Live', "\"SiteTree_Live\".\"ID\"='$page->ID'");
$this->assertNull($live);
$stage = Versioned::get_by_stage('SiteTree', 'Stage', "\"SiteTree\".\"ID\"='$page->ID'");
$this->assertNotNull($stage);
$this->assertEquals($stage->First()->Title, 'testWritingNewToStage');
Versioned::reading_stage($origStage);
}
/**
* This tests for the situation described in the ticket #5596.
* Writing new Page to live first creates a row in SiteTree table (to get the new ID), then "changes
* it's mind" in Versioned and writes SiteTree_Live. It does not remove the SiteTree record though.
*/
function testWritingNewToLive() {
$origStage = Versioned::current_stage();
Versioned::reading_stage("Live");
$page = new Page();
$page->Title = "testWritingNewToLive";
$page->URLSegment = "testWritingNewToLive";
$page->write();
$live = Versioned::get_by_stage('SiteTree', 'Live', "\"SiteTree_Live\".\"ID\"='$page->ID'");
$this->assertNotNull($live->First());
$this->assertEquals($live->First()->Title, 'testWritingNewToLive');
$stage = Versioned::get_by_stage('SiteTree', 'Stage', "\"SiteTree\".\"ID\"='$page->ID'");
$this->assertNull($stage);
Versioned::reading_stage($origStage);
}
}
class VersionedTest_DataObject extends DataObject implements TestOnly {