blob: 078fe16ade065533a664c523e929e628f685619b (
plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* 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;
}
}
|