public
Last active

  • Download Gist
Versioned.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<?php
 
// ...
 
function augmentSQL(SQLQuery &$query) {
// Get the content at a specific date
if($date = Versioned::current_archived_date()) {
foreach($query->from as $table => $dummy) {
if(!isset($baseTable)) {
$baseTable = $table;
}
 
// Only add the stage if the object is versioned
if( class_exists($table) && Object::has_extension($table, __CLASS__) ) {
$query->renameTable($table, $table . '_versions');
 
$query->replaceText("\"$table\".\"ID\"", "\"$table\".\"RecordID\"");
// Add all <basetable>_versions columns
foreach(self::$db_for_versions_table as $name => $type) {
$query->select[] = sprintf('"%s_versions"."%s"', $baseTable, $name);
}
$query->select[] = sprintf('"%s_versions"."%s" AS "ID"', $baseTable, 'RecordID');
 
if($table != $baseTable) {
$query->from[$table] .= " AND \"{$table}_versions\".\"Version\" = \"{$baseTable}_versions\".\"Version\"";
}
}
}
 
// Link to the version archived on that date
$archiveTable = $this->requireArchiveTempTable($baseTable, $date);
$query->from[$archiveTable] = "INNER JOIN \"$archiveTable\"
ON \"$archiveTable\".\"ID\" = \"{$baseTable}_versions\".\"RecordID\"
AND \"$archiveTable\".\"Version\" = \"{$baseTable}_versions\".\"Version\"";
 
// Get a specific stage
} else if(Versioned::current_stage() && Versioned::current_stage() != $this->defaultStage
&& array_search(Versioned::current_stage(), $this->stages) !== false) {
foreach($query->from as $table => $dummy) {
// Only add the stage if the object is versioned
if( class_exists($table) && Object::has_extension($table, __CLASS__) ) {
$query->renameTable($table, $table . '_' . Versioned::current_stage());
}
 
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.