Skip to content

Commit

Permalink
Merge pull request #5874 from open-sausages/pulls/4.0/fix-incorrect-v…
Browse files Browse the repository at this point in the history
…ersioned-basetable

BUG Fix incorrect use of baseClass as baseTable
  • Loading branch information
Hamish Friedlander committed Aug 10, 2016
2 parents 34c8324 + 01a13dc commit 0fa39b5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
9 changes: 5 additions & 4 deletions ORM/Versioning/Versioned.php
Expand Up @@ -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) {
Expand All @@ -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\" = ?",
Expand All @@ -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();
}
Expand Down
3 changes: 2 additions & 1 deletion filesystem/AssetControlExtension.php
Expand Up @@ -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
Expand Down
33 changes: 32 additions & 1 deletion tests/model/VersionedTest.php
@@ -1,6 +1,5 @@
<?php


use SilverStripe\ORM\DB;
use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\ORM\DataObject;
Expand All @@ -25,6 +24,7 @@ class VersionedTest extends SapphireTest {
'VersionedTest_WithIndexes',
'VersionedTest_PublicStage',
'VersionedTest_PublicViaExtension',
'VersionedTest_CustomTable',
);

protected $requiredExtensions = array(
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 0fa39b5

Please sign in to comment.