22
33namespace ProAI \Versioning ;
44
5- trait BuilderTrait {
5+ use Exception ;
66
7+ trait BuilderTrait
8+ {
79 /**
810 * Insert a new record into the database.
911 *
@@ -18,7 +20,7 @@ public function insert(array $values)
1820
1921 // set version, ref_id and latest_version
2022 $ values [$ this ->model ->getLatestVersionColumn ()] = 1 ;
21- $ versionValues [$ this ->model ->getVersionKeyName ()] = $ this ->getKey ();
23+ $ versionValues [$ this ->model ->getVersionKeyName ()] = $ this ->model -> getKey ();
2224 $ versionValues [$ this ->model ->getVersionColumn ()] = 1 ;
2325
2426 // insert main table record
@@ -88,17 +90,8 @@ public function update(array $values)
8890 return false ;
8991 }
9092
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-
10193 // update version table records
94+ $ db = $ this ->model ->getConnection ();
10295 foreach ($ affectedRecords as $ record ) {
10396 // get versioned values from record
10497 foreach ($ this ->model ->getVersionedAttributeNames () as $ key ) {
@@ -188,9 +181,18 @@ protected function getAffectedRecords()
188181 */
189182 protected function getValues (array $ values )
190183 {
191- $ versionedKeys = $ this -> model -> getVersionedAttributeNames () ;
184+ $ array = [] ;
192185
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+ }
194196
195197 return $ array ;
196198 }
@@ -203,12 +205,43 @@ protected function getValues(array $values)
203205 */
204206 protected function getVersionValues (array $ values )
205207 {
208+ $ array = [];
209+
206210 $ versionedKeys = $ this ->model ->getVersionedAttributeNames ();
207211
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+ }
210217
211218 return $ array ;
212219 }
213220
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+
214247}
0 commit comments