summaryrefslogtreecommitdiff
path: root/kohana/libraries/ORM_Versioned.php
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2008-12-17 19:28:26 +0000
committerBharat Mediratta <bharat@menalto.com>2008-12-17 19:28:26 +0000
commitfe396410894f9fcf430e31216312f70db800d96e (patch)
treefc1222c074e0f8ffd2d952688353f5bf79e360f5 /kohana/libraries/ORM_Versioned.php
parent093fb407a839b1bdacd0cd6036b349a42dcfea7d (diff)
Update Kohana code to r3799 from their svn head. All tests pass.
Resolved upstream tickets: http://dev.kohanaphp.com/ticket/972 Command: svn merge -c19275 vendor/kohana/modified/kohana trunk/kohana
Diffstat (limited to 'kohana/libraries/ORM_Versioned.php')
-rw-r--r--kohana/libraries/ORM_Versioned.php143
1 files changed, 143 insertions, 0 deletions
diff --git a/kohana/libraries/ORM_Versioned.php b/kohana/libraries/ORM_Versioned.php
new file mode 100644
index 00000000..7c3ee5d3
--- /dev/null
+++ b/kohana/libraries/ORM_Versioned.php
@@ -0,0 +1,143 @@
+<?php
+/**
+ * Object Relational Mapping (ORM) "versioned" extension. Allows ORM objects to
+ * be revisioned instead of updated.
+ *
+ * $Id$
+ *
+ * @package ORM
+ * @author Kohana Team
+ * @copyright (c) 2007-2008 Kohana Team
+ * @license http://kohanaphp.com/license.html
+ */
+class ORM_Versioned_Core extends ORM {
+
+ protected $last_version = NULL;
+
+ /**
+ * Overload ORM::save() to support versioned data
+ *
+ * @chainable
+ * @return ORM
+ */
+ public function save()
+ {
+ $this->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