|
1
|
|
|
<?php |
|
2
|
|
|
namespace Divergence\Models; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
|
|
6
|
|
|
use Divergence\Helpers\Util; |
|
7
|
|
|
use Divergence\IO\Database\MySQL as DB; |
|
8
|
|
|
|
|
9
|
|
|
trait Versioning |
|
10
|
|
|
{ |
|
11
|
|
|
public $wasDirty = false; |
|
12
|
|
|
|
|
13
|
|
|
public static $versioningFields = [ |
|
14
|
|
|
'RevisionID' => [ |
|
15
|
|
|
'columnName' => 'RevisionID', |
|
16
|
|
|
'type' => 'integer', |
|
17
|
|
|
'unsigned' => true, |
|
18
|
|
|
'notnull' => false, |
|
19
|
|
|
], |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
public static $versioningRelationships = [ |
|
23
|
|
|
'History' => [ |
|
24
|
|
|
'type' => 'history', |
|
25
|
|
|
'order' => ['RevisionID' => 'DESC'], |
|
26
|
|
|
], |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
public static function getHistoryTable() |
|
31
|
|
|
{ |
|
32
|
|
|
if (!static::$historyTable) { |
|
33
|
|
|
throw new Exception('Static variable $historyTable must be defined to use model versioning.'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return static::$historyTable; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/* |
|
40
|
|
|
* Implement specialized getters |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function getRevisionsByID($ID, $options = []) |
|
43
|
|
|
{ |
|
44
|
|
|
$options['conditions'][static::getPrimaryKey()] = $ID; |
|
45
|
|
|
|
|
46
|
|
|
return static::getRevisions($options); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function getRevisions($options = []) |
|
50
|
|
|
{ |
|
51
|
|
|
return static::instantiateRecords(static::getRevisionRecords($options)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public static function getRevisionRecords($options = []) |
|
55
|
|
|
{ |
|
56
|
|
|
$options = Util::prepareOptions($options, [ |
|
57
|
|
|
'indexField' => false, |
|
58
|
|
|
'conditions' => [], |
|
59
|
|
|
'order' => false, |
|
60
|
|
|
'limit' => false, |
|
61
|
|
|
'offset' => 0, |
|
62
|
|
|
]); |
|
63
|
|
|
|
|
64
|
|
|
$query = 'SELECT * FROM `%s` WHERE (%s)'; |
|
65
|
|
|
$params = [ |
|
66
|
|
|
static::getHistoryTable(), |
|
67
|
|
|
count($options['conditions']) ? join(') AND (', static::_mapConditions($options['conditions'])) : 1, |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
if ($options['order']) { |
|
71
|
|
|
$query .= ' ORDER BY ' . join(',', static::_mapFieldOrder($options['order'])); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if ($options['limit']) { |
|
75
|
|
|
$query .= sprintf(' LIMIT %u,%u', $options['offset'], $options['limit']); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
if ($options['indexField']) { |
|
80
|
|
|
return DB::table(static::_cn($options['indexField']), $query, $params); |
|
81
|
|
|
} else { |
|
82
|
|
|
return DB::allRecords($query, $params); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function beforeVersionedSave() |
|
87
|
|
|
{ |
|
88
|
|
|
$this->wasDirty = false; |
|
89
|
|
|
if ($this->isDirty && static::$createRevisionOnSave) { |
|
90
|
|
|
// update creation time |
|
91
|
|
|
$this->Created = time(); |
|
|
|
|
|
|
92
|
|
|
$this->wasDirty = true; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function afterVersionedSave() |
|
97
|
|
|
{ |
|
98
|
|
|
if ($this->wasDirty && static::$createRevisionOnSave) { |
|
99
|
|
|
// save a copy to history table |
|
100
|
|
|
$recordValues = $this->_prepareRecordValues(); |
|
|
|
|
|
|
101
|
|
|
$set = static::_mapValuesToSet($recordValues); |
|
102
|
|
|
DB::nonQuery( |
|
103
|
|
|
'INSERT INTO `%s` SET %s', |
|
104
|
|
|
[ |
|
105
|
|
|
static::getHistoryTable(), |
|
106
|
|
|
join(',', $set), |
|
107
|
|
|
] |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|