@@ -701,43 +701,41 @@ public function insertIfNotExists(SplArrayStorage $data, array $conditions = [])
701
701
*/
702
702
public function update (SplArrayStorage $ data , array $ conditions = [])
703
703
{
704
- if (empty ($ this ->updateValidationRules )) {
705
- if (empty ($ this ->primaryKeys )) {
706
- $ primaryKey = empty ($ this ->primaryKey ) ? 'id ' : $ this ->primaryKey ;
707
- if ($ data ->offsetExists ($ primaryKey )) {
708
- if (!array_key_exists ($ primaryKey , $ conditions )) {
704
+ if (empty ($ conditions )) {
705
+ if (count ($ this ->primaryKeys )) {
706
+ foreach ($ this ->primaryKeys as $ primaryKey ) {
707
+ if ($ data ->offsetExists ($ primaryKey )) {
709
708
$ conditions [$ primaryKey ] = $ data ->offsetGet ($ primaryKey );
710
709
}
711
- }
712
710
713
- $ this ->updateValidationRules [$ primaryKey ] = 'required ' ;
714
- $ this ->updateValidationCustomErrors [$ primaryKey ] = [
715
- 'required ' => language ('LABEL_ ' . strtoupper ($ primaryKey )) . ' cannot be empty! ' ,
716
- ];
717
- } else {
718
- foreach ($ this ->primaryKeys as $ primaryKey ) {
719
- if ($ data ->offsetExists ($ primaryKey )) {
720
- if (!array_key_exists ($ primaryKey , $ conditions )) {
721
- $ conditions [$ primaryKey ] = $ data ->offsetGet ($ primaryKey );
722
- }
711
+ if (empty ($ this ->updateValidationRules )) {
712
+ $ this ->updateValidationRules [$ primaryKey ] = 'required ' ;
713
+ $ this ->updateValidationCustomErrors [$ primaryKey ] = [
714
+ 'required ' => language ('LABEL_ ' . strtoupper ($ primaryKey )) . ' cannot be empty! ' ,
715
+ ];
723
716
}
724
-
725
- $ this ->updateValidationRules [$ primaryKey ] = 'required ' ;
726
- $ this ->updateValidationCustomErrors [$ primaryKey ] = [
727
- 'required ' => language ('LABEL_ ' . strtoupper ($ primaryKey )) . ' cannot be empty! ' ,
728
- ];
729
717
}
730
718
}
731
- } elseif (empty ($ conditions )) {
732
- if (empty ($ this ->primaryKeys )) {
733
- foreach ($ this ->primaryKeys as $ primaryKey ) {
734
- if ($ data ->offsetExists ($ primaryKey )) {
735
- $ conditions [$ primaryKey ] = $ data ->offsetGet ($ primaryKey );
719
+ }
720
+
721
+ if ($ data instanceof AbstractInput) {
722
+ if (count ($ this ->updateValidationRules )) {
723
+ $ data ->validation ($ this ->updateValidationRules , $ this ->updateValidationCustomErrors );
724
+
725
+ if (!$ data ->validate ()) {
726
+ $ this ->addErrors ($ data ->validator ->getErrors ());
727
+
728
+ if (services ()->has ('session ' ) and $ this ->flashMessage ) {
729
+ $ errors = new Unordered ();
730
+ foreach ($ data ->validator ->getErrors () as $ error ) {
731
+ $ errors ->createList ($ error );
732
+ }
733
+
734
+ session ()->setFlash ('danger ' ,
735
+ language ('FAILED_INSERT ' , $ errors ->__toString ()));
736
736
}
737
- }
738
- } else {
739
- if ($ data ->offsetExists ($ primaryKey )) {
740
- $ conditions = [$ primaryKey => $ data ->offsetGet ($ primaryKey )];
737
+
738
+ return false ;
741
739
}
742
740
}
743
741
}
@@ -877,11 +875,11 @@ public function update(SplArrayStorage $data, array $conditions = [])
877
875
$ data ['settings ' ] = new SplArrayObject ();
878
876
879
877
if (empty ($ this ->primaryKeys )) {
880
- $ ownershipId = $ data-> offsetGet ( $ this ->primaryKey ) ;
878
+ $ ownershipId = $ data[ $ this ->primaryKey ] ;
881
879
} else {
882
880
$ ownershipId = [];
883
881
foreach ($ this ->primaryKeys as $ primaryKey ) {
884
- array_push ($ ownershipId , $ data-> offsetGet ( $ this -> primaryKey ) );
882
+ array_push ($ ownershipId , $ data[ $ primaryKey] );
885
883
}
886
884
887
885
$ ownershipId = implode ('- ' , $ ownershipId );
@@ -1188,7 +1186,7 @@ private function deleteRow(Row $row)
1188
1186
} else {
1189
1187
$ ownershipId = [];
1190
1188
foreach ($ this ->primaryKeys as $ primaryKey ) {
1191
- array_push ($ ownershipId , $ data [ $ primaryKey] );
1189
+ array_push ($ ownershipId , $ row -> offsetGet ( $ primaryKey) );
1192
1190
}
1193
1191
1194
1192
$ ownershipId = implode ('- ' , $ ownershipId );
0 commit comments