Skip to content
Permalink
Browse files

BUG Fix incorrect use of baseClass as baseTable

  • Loading branch information
Damian Mooyman
Damian Mooyman committed Aug 4, 2016
1 parent f79c11f commit 01a13dcba970f9f641fb7f239b7e99582701c76d
Showing with 39 additions and 6 deletions.
  1. +5 −4 ORM/Versioning/Versioned.php
  2. +2 −1 filesystem/AssetControlExtension.php
  3. +32 −1 tests/model/VersionedTest.php
@@ -1705,14 +1705,15 @@ public function copyVersionToStage($fromStage, $toStage, $createNewVersion = fal
$owner->invokeWithExtensions('onBeforeVersionedPublish', $fromStage, $toStage, $createNewVersion);
$baseClass = $owner->baseClass();
$baseTable = $owner->baseTable();
/** @var Versioned|DataObject $from */
if(is_numeric($fromStage)) {
$from = Versioned::get_version($baseClass, $owner->ID, $fromStage);
} else {
$owner->flushCache();
$from = Versioned::get_one_by_stage($baseClass, $fromStage, array(
"\"{$baseClass}\".\"ID\" = ?" => $owner->ID
"\"{$baseTable}\".\"ID\" = ?" => $owner->ID
));
}
if(!$from) {
@@ -1728,7 +1729,7 @@ public function copyVersionToStage($fromStage, $toStage, $createNewVersion = fal
// Mark this version as having been published at some stage
$publisherID = isset(Member::currentUser()->ID) ? Member::currentUser()->ID : 0;
$extTable = $this->extendWithSuffix($baseClass);
$extTable = $this->extendWithSuffix($baseTable);
DB::prepared_query("UPDATE \"{$extTable}_versions\"
SET \"WasPublished\" = ?, \"PublisherID\" = ?
WHERE \"RecordID\" = ? AND \"Version\" = ?",
@@ -1746,9 +1747,9 @@ public function copyVersionToStage($fromStage, $toStage, $createNewVersion = fal
$conn = DB::get_conn();
if(method_exists($conn, 'allowPrimaryKeyEditing')) {
$conn->allowPrimaryKeyEditing($baseClass, true);
$conn->allowPrimaryKeyEditing($baseTable, true);
$from->write();
$conn->allowPrimaryKeyEditing($baseClass, false);
$conn->allowPrimaryKeyEditing($baseTable, false);
} else {
$from->write();
}
@@ -162,7 +162,8 @@ protected function addAssetsFromOtherStages(AssetManipulationList $manipulation)
// Unauthenticated member to use for checking visibility
$baseClass = $this->owner->baseClass();
$filter = array("\"{$baseClass}\".\"ID\"" => $this->owner->ID);
$baseTable = $this->owner->baseTable();
$filter = array("\"{$baseTable}\".\"ID\"" => $this->owner->ID);
$stages = $this->owner->getVersionedStages(); // {@see Versioned::getVersionedStages}
foreach ($stages as $stage) {
// Skip current stage; These should be handled explicitly
@@ -1,6 +1,5 @@
<?php
use SilverStripe\ORM\DB;
use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\ORM\DataObject;
@@ -25,6 +24,7 @@ class VersionedTest extends SapphireTest {
'VersionedTest_WithIndexes',
'VersionedTest_PublicStage',
'VersionedTest_PublicViaExtension',
'VersionedTest_CustomTable',
);
protected $requiredExtensions = array(
@@ -110,6 +110,22 @@ public function testDeletingOrphanedVersions() {
$this->assertEquals($count, $count2);
}
public function testCustomTable() {
$obj = new VersionedTest_CustomTable();
$obj->Title = 'my object';
$obj->write();
$id = $obj->ID;
$obj->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$obj->Title = 'new title';
$obj->write();
$liveRecord = Versioned::get_by_stage('VersionedTest_CustomTable', Versioned::LIVE)->byID($id);
$draftRecord = Versioned::get_by_stage('VersionedTest_CustomTable', Versioned::DRAFT)->byID($id);
$this->assertEquals('my object', $liveRecord->Title);
$this->assertEquals('new title', $draftRecord->Title);
}
/**
* Test that publishing from invalid stage will throw exception
*/
@@ -1242,3 +1258,18 @@ public function canViewNonLive($member = null) {
return true;
}
}
/**
* @mixin Versioned
*/
class VersionedTest_CustomTable extends DataObject implements TestOnly {
private static $db = [
'Title' => 'Varchar'
];
private static $table_name = 'VTCustomTable';
private static $extensions = [
"SilverStripe\\ORM\\Versioning\\Versioned",
];
}

0 comments on commit 01a13dc

Please sign in to comment.
You can’t perform that action at this time.