From 3413fe6bfd423d45d083ff4ed62c0f72c2cc272f Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 28 May 2009 06:11:53 +0800 Subject: Rename 'kohana' to 'system' to conform to the Kohana filesystem layout. I'm comfortable with us not clearly drawing the distinction about the fact that it's Kohana. Signed-off-by: Gallery Role Account --- system/libraries/ORM_Versioned.php | 143 +++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 system/libraries/ORM_Versioned.php (limited to 'system/libraries/ORM_Versioned.php') diff --git a/system/libraries/ORM_Versioned.php b/system/libraries/ORM_Versioned.php new file mode 100644 index 00000000..078fe16a --- /dev/null +++ b/system/libraries/ORM_Versioned.php @@ -0,0 +1,143 @@ +last_version = 1 + ($this->last_version === NULL ? $this->object['version'] : $this->last_version); + $this->__set('version', $this->last_version); + + parent::save(); + + if ($this->saved) + { + $data = array(); + foreach ($this->object as $key => $value) + { + if ($key === 'id') + continue; + + $data[$key] = $value; + } + $data[$this->foreign_key()] = $this->id; + + $this->db->insert($this->table_name.'_versions', $data); + } + + return $this; + } + + /** + * Loads previous version from current object + * + * @chainable + * @return ORM + */ + public function previous() + { + if ( ! $this->loaded) + return $this; + + $this->last_version = ($this->last_version === NULL) ? $this->object['version'] : $this->last_version; + $version = $this->last_version - 1; + + $query = $this->db + ->where($this->foreign_key(), $this->object[$this->primary_key]) + ->where('version', $version) + ->limit(1) + ->get($this->table_name.'_versions'); + + if ($query->count()) + { + $this->load_values($query->result(FALSE)->current()); + } + + return $this; + } + + /** + * Restores the object with data from stored version + * + * @param integer version number you want to restore + * @return ORM + */ + public function restore($version) + { + if ( ! $this->loaded) + return $this; + + $query = $this->db + ->where($this->foreign_key(), $this->object[$this->primary_key]) + ->where('version', $version) + ->limit(1) + ->get($this->table_name.'_versions'); + + if ($query->count()) + { + $row = $query->result(FALSE)->current(); + + foreach ($row as $key => $value) + { + if ($key === $this->primary_key OR $key === $this->foreign_key()) + { + // Do not overwrite the primary key + continue; + } + + if ($key === 'version') + { + // Always use the current version + $value = $this->version; + } + + $this->__set($key, $value); + } + + $this->save(); + } + + return $this; + } + + /** + * Overloads ORM::delete() to delete all versioned entries of current object + * and the object itself + * + * @param integer id of the object you want to delete + * @return ORM + */ + public function delete($id = NULL) + { + if ($id === NULL) + { + // Use the current object id + $id = $this->object[$this->primary_key]; + } + + if ($status = parent::delete($id)) + { + $this->db->where($this->foreign_key(), $id)->delete($this->table_name.'_versions'); + } + + return $status; + } + +} \ No newline at end of file -- cgit v1.2.3