2
2
3
3
namespace ProAI \Versioning ;
4
4
5
- trait BuilderTrait {
5
+ use Exception ;
6
6
7
+ trait BuilderTrait
8
+ {
7
9
/**
8
10
* Insert a new record into the database.
9
11
*
@@ -18,7 +20,7 @@ public function insert(array $values)
18
20
19
21
// set version, ref_id and latest_version
20
22
$ values [$ this ->model ->getLatestVersionColumn ()] = 1 ;
21
- $ versionValues [$ this ->model ->getVersionKeyName ()] = $ this ->getKey ();
23
+ $ versionValues [$ this ->model ->getVersionKeyName ()] = $ this ->model -> getKey ();
22
24
$ versionValues [$ this ->model ->getVersionColumn ()] = 1 ;
23
25
24
26
// insert main table record
@@ -88,17 +90,8 @@ public function update(array $values)
88
90
return false ;
89
91
}
90
92
91
- // update all versions in case primary key value has been changed
92
- $ db = $ this ->model ->getConnection ();
93
- if ($ this ->model ->getKey () && $ this ->model ->getKey () != $ this ->model ->getOriginal ($ this ->model ->getKeyName ())) {
94
- if (! $ db ->table ($ this ->model ->getVersionTable ())
95
- ->where ($ this ->model ->getVersionKeyName (), '= ' , $ this ->model ->getOriginal ($ this ->model ->getKeyName ()))
96
- ->update ([$ this ->model ->getVersionKeyName () => $ this ->model ->getKey ()])) {
97
- return false ;
98
- }
99
- }
100
-
101
93
// update version table records
94
+ $ db = $ this ->model ->getConnection ();
102
95
foreach ($ affectedRecords as $ record ) {
103
96
// get versioned values from record
104
97
foreach ($ this ->model ->getVersionedAttributeNames () as $ key ) {
@@ -188,9 +181,18 @@ protected function getAffectedRecords()
188
181
*/
189
182
protected function getValues (array $ values )
190
183
{
191
- $ versionedKeys = $ this -> model -> getVersionedAttributeNames () ;
184
+ $ array = [] ;
192
185
193
- $ array = array_diff_key ($ values , array_flip ($ versionedKeys ));
186
+ $ versionedKeys = array_merge (
187
+ $ this ->model ->getVersionedAttributeNames (),
188
+ [$ this ->model ->getLatestVersionColumn (), $ this ->model ->getVersionColumn (), $ this ->model ->getVersionKeyName ()]
189
+ );
190
+
191
+ foreach ($ values as $ key => $ value ) {
192
+ if (! $ this ->isVersionedKey ($ key , $ versionedKeys )) {
193
+ $ array [$ key ] = $ value ;
194
+ }
195
+ }
194
196
195
197
return $ array ;
196
198
}
@@ -203,12 +205,43 @@ protected function getValues(array $values)
203
205
*/
204
206
protected function getVersionValues (array $ values )
205
207
{
208
+ $ array = [];
209
+
206
210
$ versionedKeys = $ this ->model ->getVersionedAttributeNames ();
207
211
208
- $ array = array_intersect_key ($ values , array_flip ($ versionedKeys ));
209
- $ array [$ this ->model ->getVersionKeyName ()] = $ this ->model ->getKey ();
212
+ foreach ($ values as $ key => $ value ) {
213
+ if ($ newKey = $ this ->isVersionedKey ($ key , $ versionedKeys )) {
214
+ $ array [$ newKey ] = $ value ;
215
+ }
216
+ }
210
217
211
218
return $ array ;
212
219
}
213
220
221
+ /**
222
+ * Check if key is in versioned keys.
223
+ *
224
+ * @param string $key
225
+ * @param array $versionedKeys
226
+ * @return string|null
227
+ */
228
+ protected function isVersionedKey ($ key , array $ versionedKeys )
229
+ {
230
+ $ keyFractions = explode (". " ,$ key );
231
+
232
+ if (count ($ keyFractions ) > 2 ) {
233
+ throw new Exception ("Key ' " .$ key ."' has too many fractions. " );
234
+ }
235
+
236
+ if (count ($ keyFractions ) == 1 && in_array ($ keyFractions [0 ], $ versionedKeys )) {
237
+ return $ keyFractions [0 ];
238
+ }
239
+
240
+ if (count ($ keyFractions ) == 2 && $ keyFractions [0 ] == $ this ->model ->getVersionTable () && in_array ($ keyFractions [1 ], $ versionedKeys )) {
241
+ return $ keyFractions [1 ];
242
+ }
243
+
244
+ return null ;
245
+ }
246
+
214
247
}
0 commit comments