diff --git a/model/Versioned.php b/model/Versioned.php index 9ff961f62..caf87ad99 100644 --- a/model/Versioned.php +++ b/model/Versioned.php @@ -616,7 +616,11 @@ class Versioned extends DataExtension { } // If we're editing Live, then use (table)_Live instead of (table) - if(Versioned::current_stage() && Versioned::current_stage() != $this->defaultStage) { + if( + Versioned::current_stage() + && Versioned::current_stage() != $this->defaultStage + && in_array(Versioned::current_stage(), $this->stages) + ) { // 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'"); diff --git a/tests/model/VersionedTest.php b/tests/model/VersionedTest.php index 588272621..6c1849c93 100644 --- a/tests/model/VersionedTest.php +++ b/tests/model/VersionedTest.php @@ -11,7 +11,8 @@ class VersionedTest extends SapphireTest { protected $extraDataObjects = array( 'VersionedTest_DataObject', 'VersionedTest_Subclass', - 'VersionedTest_RelatedWithoutVersion' + 'VersionedTest_RelatedWithoutVersion', + 'VersionedTest_SingleStage' ); protected $requiredExtensions = array( @@ -452,6 +453,46 @@ class VersionedTest extends SapphireTest { ); } + public function testVersionedWithSingleStage() { + $tables = DB::tableList(); + $this->assertContains( + 'VersionedTest_SingleStage', + array_values($tables), + 'Contains base table' + ); + $this->assertContains( + 'VersionedTest_SingleStage_versions', + array_values($tables), + 'Contains versions table' + ); + $this->assertNotContains( + 'VersionedTest_SingleStage_Live', + array_values($tables), + 'Does not contain separate table with _Live suffix' + ); + $this->assertNotContains( + 'VersionedTest_SingleStage_Stage', + array_values($tables), + 'Does not contain separate table with _Stage suffix' + ); + + Versioned::reading_stage("Stage"); + $obj = new VersionedTest_SingleStage(array('Name' => 'MyObj')); + $obj->write(); + $this->assertNotNull( + VersionedTest_SingleStage::get()->byID($obj->ID), + 'Writes to and reads from default stage if its set explicitly' + ); + + Versioned::reading_stage("Live"); + $obj = new VersionedTest_SingleStage(array('Name' => 'MyObj')); + $obj->write(); + $this->assertNotNull( + VersionedTest_SingleStage::get()->byID($obj->ID), + 'Writes to and reads from default stage even if a non-matching stage is set' + ); + } + } @@ -517,3 +558,13 @@ class VersionedTest_Subclass extends VersionedTest_DataObject implements TestOnl class VersionedTest_UnversionedWithField extends DataObject implements TestOnly { private static $db = array('Version' => 'Varchar(255)'); } + +class VersionedTest_SingleStage extends DataObject implements TestOnly { + private static $db = array( + 'Name' => 'Varchar' + ); + + private static $extensions = array( + 'Versioned("Stage")' + ); +} \ No newline at end of file