@@ -546,6 +546,8 @@ protected function getHash($str, $len)
546546 */
547547 public function save ($ data )
548548 {
549+ $ saveData = $ data ;
550+
549551 // If $data is using a custom class with public or protected
550552 // properties representing the table elements, we need to grab
551553 // them as an array.
@@ -556,14 +558,27 @@ public function save($data)
556558
557559 if (is_object ($ data ) && isset ($ data ->{$ this ->primaryKey }))
558560 {
559- return $ this ->update ($ data ->{$ this ->primaryKey }, $ data );
561+ $ response = $ this ->update ($ data ->{$ this ->primaryKey }, $ data );
560562 }
561563 elseif (is_array ($ data ) && ! empty ($ data [$ this ->primaryKey ]))
562564 {
563- return $ this ->update ($ data [$ this ->primaryKey ], $ data );
565+ $ response = $ this ->update ($ data [$ this ->primaryKey ], $ data );
564566 }
567+ else
568+ {
569+ $ response = $ this ->insert ($ data );
570+ }
571+
572+ // If it was an Entity class, check it for an onSave method.
573+ if (is_object ($ saveData ) && ! $ saveData instanceof \stdClass)
574+ {
575+ if (method_exists ($ saveData , 'onSave ' ))
576+ {
577+ $ saveData ->onSave ();
578+ }
579+ }
565580
566- return $ this -> insert ( $ data ) ;
581+ return $ response ;
567582 }
568583
569584 //--------------------------------------------------------------------
@@ -607,6 +622,14 @@ protected function classToArray($data): array
607622 */
608623 public function insert ($ data )
609624 {
625+ // If $data is using a custom class with public or protected
626+ // properties representing the table elements, we need to grab
627+ // them as an array.
628+ if (is_object ($ data ) && ! $ data instanceof \stdClass)
629+ {
630+ $ data = $ this ->classToArray ($ data );
631+ }
632+
610633 // Validate data before saving.
611634 if ($ this ->skipValidation === false )
612635 {
@@ -653,6 +676,14 @@ public function insert($data)
653676 */
654677 public function update ($ id , $ data )
655678 {
679+ // If $data is using a custom class with public or protected
680+ // properties representing the table elements, we need to grab
681+ // them as an array.
682+ if (is_object ($ data ) && ! $ data instanceof \stdClass)
683+ {
684+ $ data = $ this ->classToArray ($ data );
685+ }
686+
656687 // Validate data before saving.
657688 if ($ this ->skipValidation === false )
658689 {
0 commit comments