1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Object Relational Mapping (ORM) "versioned" extension. Allows ORM objects to |
4
|
|
|
* be revisioned instead of updated. |
5
|
|
|
* |
6
|
|
|
* $Id$ |
7
|
|
|
* |
8
|
|
|
* @package ORM |
9
|
|
|
* @author Kohana Team |
10
|
|
|
* @copyright (c) 2007-2008 Kohana Team |
11
|
|
|
* @license http://kohanaphp.com/license.html |
12
|
|
|
*/ |
13
|
|
|
class ORM_Versioned_Core extends ORM |
14
|
|
|
{ |
15
|
|
|
protected $last_version = null; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Overload ORM::save() to support versioned data |
19
|
|
|
* |
20
|
|
|
* @chainable |
21
|
|
|
* @return ORM |
|
|
|
|
22
|
|
|
*/ |
23
|
|
|
public function save() |
24
|
|
|
{ |
25
|
|
|
$this->last_version = 1 + ($this->last_version === null ? $this->object['version'] : $this->last_version); |
26
|
|
|
$this->__set('version', $this->last_version); |
27
|
|
|
|
28
|
|
|
parent::save(); |
29
|
|
|
|
30
|
|
|
if ($this->saved) { |
31
|
|
|
$data = array(); |
32
|
|
|
foreach ($this->object as $key => $value) { |
33
|
|
|
if ($key === 'id') { |
34
|
|
|
continue; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$data[$key] = $value; |
38
|
|
|
} |
39
|
|
|
$data[$this->foreign_key()] = $this->id; |
40
|
|
|
|
41
|
|
|
$this->db->insert($this->table_name.'_versions', $data); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Loads previous version from current object |
49
|
|
|
* |
50
|
|
|
* @chainable |
51
|
|
|
* @return ORM |
|
|
|
|
52
|
|
|
*/ |
53
|
|
|
public function previous() |
54
|
|
|
{ |
55
|
|
|
if (! $this->loaded) { |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->last_version = ($this->last_version === null) ? $this->object['version'] : $this->last_version; |
60
|
|
|
$version = $this->last_version - 1; |
61
|
|
|
|
62
|
|
|
$query = $this->db |
63
|
|
|
->where($this->foreign_key(), $this->object[$this->primary_key]) |
64
|
|
|
->where('version', $version) |
65
|
|
|
->limit(1) |
66
|
|
|
->get($this->table_name.'_versions'); |
67
|
|
|
|
68
|
|
|
if ($query->count()) { |
69
|
|
|
$this->load_values($query->result(false)->current()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Restores the object with data from stored version |
77
|
|
|
* |
78
|
|
|
* @param integer version number you want to restore |
79
|
|
|
* @return ORM |
|
|
|
|
80
|
|
|
*/ |
81
|
|
|
public function restore($version) |
82
|
|
|
{ |
83
|
|
|
if (! $this->loaded) { |
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$query = $this->db |
88
|
|
|
->where($this->foreign_key(), $this->object[$this->primary_key]) |
89
|
|
|
->where('version', $version) |
90
|
|
|
->limit(1) |
91
|
|
|
->get($this->table_name.'_versions'); |
92
|
|
|
|
93
|
|
|
if ($query->count()) { |
94
|
|
|
$row = $query->result(false)->current(); |
95
|
|
|
|
96
|
|
|
foreach ($row as $key => $value) { |
97
|
|
|
if ($key === $this->primary_key or $key === $this->foreign_key()) { |
98
|
|
|
// Do not overwrite the primary key |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($key === 'version') { |
103
|
|
|
// Always use the current version |
104
|
|
|
$value = $this->version; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->__set($key, $value); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->save(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Overloads ORM::delete() to delete all versioned entries of current object |
118
|
|
|
* and the object itself |
119
|
|
|
* |
120
|
|
|
* @param integer id of the object you want to delete |
121
|
|
|
* @return ORM |
122
|
|
|
*/ |
123
|
|
|
public function delete($id = null) |
124
|
|
|
{ |
125
|
|
|
if ($id === null) { |
126
|
|
|
// Use the current object id |
127
|
|
|
$id = $this->object[$this->primary_key]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($status = parent::delete($id)) { |
131
|
|
|
$this->db->where($this->foreign_key(), $id)->delete($this->table_name.'_versions'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $status; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.