Permalink
Browse files

- Clean out obsolete versions of objects in versions table on after w…

…rite and delete.
  • Loading branch information...
jamesbolitho committed Mar 19, 2012
1 parent 301f845 commit ff93c6780a3ddac00d9047050f621dd38fd3e98f
Showing with 18 additions and 0 deletions.
  1. +18 −0 code/DataObjects/DataObjectAsPage.php
@@ -298,6 +298,8 @@ function doDelete() {
$oldMode = Versioned::get_reading_mode();
Versioned::reading_stage('Draft');
+ //delete all versioned objects with this ID
+ $result = DB::query("DELETE FROM DataObjectAsPage_versions WHERE RecordID = '$this->ID'");
$result = $this->delete();
Versioned::set_reading_mode($oldMode);
@@ -419,6 +421,22 @@ public function onBeforeWrite()
$this->URLSegment = $URLSegment;
}
+ function onAfterWrite() {
+ parent::onAfterWrite();
+ // Clear out obselete versions of records since there is no way to role back to previous versions yet.
+ if(DB::query("SELECT \"ID\" FROM \"DataObjectAsPage\" WHERE \"ID\" = $this->ID")->value()) {
+
+ $LiveVersionID = DB::query("SELECT \"Version\" FROM \"DataObjectAsPage_Live\" WHERE \"ID\" = $this->ID")->value();
+ $DraftVersionID = DB::query("SELECT \"Version\" FROM \"DataObjectAsPage\" WHERE \"ID\" = $this->ID")->value();
+
+ if($LiveVersionID){
+ DB::query("DELETE FROM DataObjectAsPage_versions WHERE RecordID = $this->ID AND Version != '" . $DraftVersionID . "' AND Version != '" . $LiveVersionID . "'");
+ } else {
+ DB::query("DELETE FROM DataObjectAsPage_versions WHERE RecordID = $this->ID AND Version != '" . $DraftVersionID . "'");
+ }
+ }
+ }
+
//Test whether the URLSegment exists already on another Item
public function LookForExistingURLSegment($URLSegment, $ID)
{

0 comments on commit ff93c67

Please sign in to comment.