@@ -54,50 +54,53 @@ public function saveAll() {
5454 try {
5555 if ($ this ->save ()) {
5656 $ error = 0 ;
57- if (!empty ($ this ->relatedRecords )) {
58- foreach ($ this ->relatedRecords as $ name => $ records ) {
59- $ AQ = $ this ->getRelation ($ name );
60- $ link = $ AQ ->link ;
61- $ notDeletedPK = [];
62- $ relPKAttr = $ records [0 ]->primaryKey ();
63- $ isCompositePK = (count ($ relPKAttr ) > 1 );
64- /* @var $relModel ActiveRecord */
65- foreach ($ records as $ index => $ relModel ){
66- foreach ($ link as $ key => $ value ){
67- $ relModel ->$ key = $ this ->$ value ;
68- $ notDeletedFK [$ key ] = "$ key = ' {$ this ->$ value }' " ;
69- }
70- if (!$ relModel ->save ()){
71- $ relModelWords = Inflector::camel2words (StringHelper::basename ($ AQ ->modelClass ));
72- $ index ++;
73- foreach ($ relModel ->errors as $ validation ){
74- foreach ($ validation as $ errorMsg ){
75- $ this ->addError ($ name ,"$ relModelWords # $ index : $ errorMsg " );
76- }
57+ foreach ($ this ->relatedRecords as $ name => $ records ) {
58+ $ AQ = $ this ->getRelation ($ name );
59+ $ link = $ AQ ->link ;
60+ $ notDeletedPK = [];
61+ $ relPKAttr = $ records [0 ]->primaryKey ();
62+ $ isCompositePK = (count ($ relPKAttr ) > 1 );
63+ /* @var $relModel ActiveRecord */
64+ foreach ($ records as $ index => $ relModel ){
65+ foreach ($ link as $ key => $ value ){
66+ $ relModel ->$ key = $ this ->$ value ;
67+ $ notDeletedFK [$ key ] = "$ key = ' {$ this ->$ value }' " ;
68+ }
69+ $ relSave = $ relModel ->save ();
70+ if (!$ relSave ){
71+ $ relModelWords = Inflector::camel2words (StringHelper::basename ($ AQ ->modelClass ));
72+ $ index ++;
73+ foreach ($ relModel ->errors as $ validation ){
74+ foreach ($ validation as $ errorMsg ){
75+ $ this ->addError ($ name ,"$ relModelWords # $ index : $ errorMsg " );
7776 }
78- $ error = 1 ;
79- }else {
80- //GET PK OF REL MODEL
81- if ($ isCompositePK ){
82- foreach ($ relModel ->primaryKey as $ attr => $ value ){
83- $ notDeletedPK [$ attr ][] = "' $ value' " ;
84- }
85- } else {
86- $ notDeletedPK [] = "' $ relModel ->primaryKey ' " ;
77+ }
78+ $ error = 1 ;
79+ }else {
80+ //GET PK OF REL MODEL
81+ if ($ isCompositePK ){
82+ foreach ($ relModel ->primaryKey as $ attr => $ value ){
83+ $ notDeletedPK [$ attr ][] = "' $ value' " ;
8784 }
85+ } else {
86+ $ notDeletedPK [] = "' $ relModel ->primaryKey ' " ;
8887 }
8988 }
90- if (!empty ($ notDeletedPK ) && !empty ($ notDeletedFK )){
91- //DELETE WITH 'NOT IN' PK MODEL & REL MODEL
92- $ notDeletedFK = implode (' AND ' , $ notDeletedFK );
93- if ($ isCompositePK ){
94- $ compiledNotDeletedPK = [];
95- foreach ($ notDeletedPK as $ attr => $ pks ){
96- $ compiledNotDeletedPK [$ attr ] = "$ attr NOT IN( " .implode (', ' , $ pks ).") " ;
89+ }
90+ if (!$ this ->isNewRecord ){
91+ //DELETE WITH 'NOT IN' PK MODEL & REL MODEL
92+ $ notDeletedFK = implode (' AND ' , $ notDeletedFK );
93+ if ($ isCompositePK ){
94+ $ compiledNotDeletedPK = [];
95+ foreach ($ notDeletedPK as $ attr => $ pks ){
96+ $ compiledNotDeletedPK [$ attr ] = "$ attr NOT IN( " .implode (', ' , $ pks ).") " ;
97+ if (!empty ($ compiledNotDeletedPK [$ attr ])){
9798 $ relModel ->deleteAll ("$ notDeletedFK AND " .implode (' AND ' , $ compiledNotDeletedPK ));
9899 }
99- }else {
100- $ compiledNotDeletedPK = implode (', ' , $ notDeletedPK );
100+ }
101+ }else {
102+ $ compiledNotDeletedPK = implode (', ' , $ notDeletedPK );
103+ if (!empty ($ compiledNotDeletedPK )){
101104 $ relModel ->deleteAll ($ notDeletedFK .' AND ' .$ relPKAttr [0 ]." NOT IN ( $ compiledNotDeletedPK) " );
102105 }
103106 }
0 commit comments