Skip to content

Instantly share code, notes, and snippets.

@markjames
Created February 14, 2012 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markjames/1828664 to your computer and use it in GitHub Desktop.
Save markjames/1828664 to your computer and use it in GitHub Desktop.
<?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());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment