@@ -35,9 +35,16 @@ abstract class Model extends ContainerAwareObject implements \ArrayAccess, \Iter
3535 */
3636 public $ tablename ;
3737
38- /** @var boolean is first save flag */
38+ /**
39+ * @var boolean first save flag
40+ */
3941 protected $ is_first_save ;
4042
43+ /**
44+ * @var array|null original model data
45+ */
46+ private $ original_data = null ;
47+
4148 /**
4249 * {@inheritdocs}
4350 *
@@ -240,6 +247,8 @@ public function fill($id)
240247 $ this ->dbrow = $ dbrow ;
241248 }
242249
250+ $ this ->is_first_save = $ this ->isNew ();
251+
243252 return $ this ;
244253 }
245254
@@ -288,9 +297,12 @@ public function reset()
288297 $ dbrow = $ this ->getDb ()->table ($ this ->dbrow ->getTable (), $ this ->dbrow ->getOriginalId ());
289298 if ($ dbrow ) {
290299 $ this ->dbrow = $ dbrow ;
300+ $ this ->original_data = $ this ->dbrow ->getData ();
291301 }
292302 }
293303
304+ $ this ->is_first_save = $ this ->isNew ();
305+
294306 return $ this ;
295307 }
296308
@@ -305,6 +317,7 @@ public static function load(ContainerInterface $container, $id)
305317 {
306318 $ dbrow = $ container ->get ('db ' )->table (static ::defaultTableName (), $ id );
307319 $ object = new static ($ container , $ dbrow );
320+ $ object ->setOriginalData ($ dbrow ->getData ());
308321 $ object ->setIsFirstSave (false );
309322 return $ object ;
310323 }
@@ -320,6 +333,7 @@ public static function new(ContainerInterface $container, $initialdata = [])
320333 $ dbrow = $ container ->get ('db ' )->createRow (static ::defaultTableName ());
321334 $ dbrow ->setData ($ initialdata );
322335 $ object = new static ($ container , $ dbrow );
336+ $ object ->setOriginalData (null );
323337 $ object ->setIsFirstSave (true );
324338 return $ object ;
325339 }
@@ -336,6 +350,7 @@ public static function loadBy(ContainerInterface $container, $field, $value)
336350 {
337351 $ dbrow = $ container ->get ('db ' )->table (static ::defaultTableName ())->where ($ field , $ value )->limit (1 )->fetch ();
338352 $ object = new static ($ container , $ dbrow );
353+ $ object ->setOriginalData ($ dbrow ->getData ());
339354 $ object ->setIsFirstSave (false );
340355 return $ object ;
341356 }
@@ -521,6 +536,8 @@ public function persist()
521536
522537 $ this ->setIsFirstSave (false );
523538
539+ $ this ->original_data = $ this ->dbrow ->getData ();
540+
524541 return $ this ;
525542 }
526543
@@ -588,22 +605,47 @@ public function postRemove()
588605 return $ this ;
589606 }
590607
591- /**
592- *
593- * [isFirstSave description]
594- * */
595608 public function setIsFirstSave ($ is_first_save )
596609 {
597610 $ this ->is_first_save = $ is_first_save ;
598611 return $ this ;
599612 }
600613
601- /**
602- *
603- * [isFirstSave description]
604- * */
605614 public function isFirstSave ()
606615 {
607616 return ($ this ->is_first_save == true );
608617 }
618+
619+
620+ protected function setOriginalData ($ original_data )
621+ {
622+ $ this ->original_data = $ original_data ;
623+ return $ this ;
624+ }
625+
626+ protected function getOriginalData ($ key = null )
627+ {
628+ if ($ key != null && array_key_exists ($ key , $ this ->original_data )) {
629+ return $ this ->original_data [$ key ];
630+ }
631+
632+ return $ this ->original_data ;
633+ }
634+
635+ public function getChangedData ()
636+ {
637+ if ($ this ->getOriginalData () == null ) {
638+ return $ this ->getData ();
639+ }
640+
641+ $ changed = [];
642+
643+ foreach ($ this ->getData () as $ key => $ value ) {
644+ if ($ this ->getOriginalData ($ key ) != $ value ) {
645+ $ changed [$ key ] = ['now ' => $ value , 'original ' => $ this ->getOriginalData ($ key )];
646+ }
647+ }
648+
649+ return $ changed ;
650+ }
609651}
0 commit comments