@@ -33,7 +33,7 @@ public function loadAll($POST, $skippedRelations = [])
3333 $ relPKAttr = $ relModelClass ::primaryKey ();
3434 $ isManyMany = count ($ relPKAttr ) > 1 ;
3535
36- if (in_array ($ relName , $ skippedRelations ))
36+ if (in_array ($ relName , $ skippedRelations ))
3737 continue ;
3838
3939 if ($ isManyMany ) {
@@ -91,7 +91,7 @@ public function saveAll($skippedRelations = [])
9191 if (!empty ($ this ->relatedRecords )) {
9292 foreach ($ this ->relatedRecords as $ name => $ records ) {
9393
94- if (in_array ($ name , $ skippedRelations ))
94+ if (in_array ($ name , $ skippedRelations ))
9595 continue ;
9696
9797 if (!empty ($ records )) {
@@ -107,8 +107,8 @@ public function saveAll($skippedRelations = [])
107107 $ notDeletedFK = [];
108108 foreach ($ link as $ key => $ value ) {
109109 $ relModel ->$ key = $ this ->$ value ;
110- if ($ isManyMany ) $ notDeletedFK [$ key ] = $ this ->$ value ;
111- elseif ($ AQ ->multiple ) $ notDeletedFK [$ key ] = "$ key = ' {$ this ->$ value }' " ;
110+ if ($ isManyMany ) $ notDeletedFK [$ key ] = $ this ->$ value ;
111+ elseif ($ AQ ->multiple ) $ notDeletedFK [$ key ] = "$ key = ' {$ this ->$ value }' " ;
112112 }
113113 $ relSave = $ relModel ->save ();
114114
@@ -138,23 +138,23 @@ public function saveAll($skippedRelations = [])
138138 //DELETE WITH 'NOT IN' PK MODEL & REL MODEL
139139 if ($ isManyMany ) {
140140 $ compiledFields = implode (", " , array_keys ($ fields ));
141- $ compiledNotDeletedPK = ['and ' ,$ notDeletedFK ];
141+ $ compiledNotDeletedPK = ['and ' , $ notDeletedFK ];
142142 $ notIn = ['not in ' , new \yii \db \Expression ("( $ compiledFields) " )];
143- foreach ($ notDeletedPK as $ value ){
143+ foreach ($ notDeletedPK as $ value ) {
144144 $ v = [];
145- foreach ($ fields as $ key => $ f ){
145+ foreach ($ fields as $ key => $ f ) {
146146 $ v [] = $ value [$ key ];
147147 }
148- $ c = implode (', ' ,$ v );
148+ $ c = implode (', ' , $ v );
149149 $ content [] = new \yii \db \Expression ("( $ c) " );
150150 }
151- array_push ($ notIn ,$ content );
152- array_push ($ compiledNotDeletedPK ,$ notIn );
151+ array_push ($ notIn , $ content );
152+ array_push ($ compiledNotDeletedPK , $ notIn );
153153 $ relModel ->deleteAll ($ compiledNotDeletedPK );
154- try {
154+ try {
155155 $ relModel ->deleteAll ($ compiledNotDeletedPK );
156156 } catch (\yii \db \IntegrityException $ exc ) {
157- $ this ->addError ($ name , \Yii::t ('app ' , "Data can't be deleted because it's still used by another data. " ));
157+ $ this ->addError ($ name , \Yii::t ('mtrelt ' , "Data can't be deleted because it's still used by another data. " ));
158158 $ error = true ;
159159 }
160160 } else {
@@ -164,7 +164,7 @@ public function saveAll($skippedRelations = [])
164164 try {
165165 $ relModel ->deleteAll ($ notDeletedFK . ' AND ' . $ relPKAttr [0 ] . " NOT IN ( $ compiledNotDeletedPK) " );
166166 } catch (\yii \db \IntegrityException $ exc ) {
167- $ this ->addError ($ name , \Yii::t ('app ' , "Data can't be deleted because it's still used by another data. " ));
167+ $ this ->addError ($ name , \Yii::t ('mtrelt ' , "Data can't be deleted because it's still used by another data. " ));
168168 $ error = true ;
169169 }
170170 }
@@ -188,40 +188,42 @@ public function saveAll($skippedRelations = [])
188188 }
189189 }
190190 }
191- } else {
192- //No Children left
193- if (!$ isNewRecord ) {
194- $ relData = $ this ->getRelationData ();
195- foreach ($ relData as $ rel ) {
196- if (in_array ($ rel ['name ' ], $ skippedRelations ))
197- continue ;
198- /* @var $relModel ActiveRecord */
199- if (empty ($ rel ['via ' ])){
200- $ relModel = new $ rel ['modelClass ' ];
201- $ condition = [];
202- $ isManyMany = count ($ relModel ->primaryKey ()) > 1 ;
203- if ($ isManyMany ){
204- foreach ($ rel ['link ' ] as $ k => $ v ) {
205- $ condition [] = "$ k = ' {$ this ->$ v }' " ;
206- }
207- try {
208- $ relModel ->deleteAll (implode (" AND " , $ condition ));
209- } catch (\yii \db \IntegrityException $ exc ) {
210- $ this ->addError ($ rel ['name ' ], \Yii::t ('app ' , "Data can't be deleted because it's still used by another data. " ));
211- $ error = true ;
212- }
213- }else {
214- if ($ rel ['ismultiple ' ]){
215- foreach ($ rel ['link ' ] as $ k => $ v ) {
216- $ condition [] = "$ k = ' {$ this ->$ v }' " ;
217- }
218- try {
219- $ relModel ->deleteAll (implode (" AND " , $ condition ));
220- } catch (\yii \db \IntegrityException $ exc ) {
221- $ this ->addError ($ rel ['name ' ], \Yii::t ('app ' , "Data can't be deleted because it's still used by another data. " ));
222- $ error = true ;
223- }
224- }
191+ }
192+ //No Children left
193+ $ relAvail = array_keys ($ this ->relatedRecords );
194+ $ relData = $ this ->getRelationData ();
195+ $ allRel = array_keys ($ relData );
196+ $ noChildren = array_diff ($ allRel , $ relAvail );
197+
198+ foreach ($ noChildren as $ relName ) {
199+ if (in_array ($ relName , $ skippedRelations )) {
200+ continue ;
201+ }
202+ /* @var $relModel ActiveRecord */
203+ if (empty ($ relData [$ relName ]['via ' ])) {
204+ $ relModel = new $ relData [$ relName ]['modelClass ' ];
205+ $ condition = [];
206+ $ isManyMany = count ($ relModel ->primaryKey ()) > 1 ;
207+ if ($ isManyMany ) {
208+ foreach ($ relData [$ relName ]['link ' ] as $ k => $ v ) {
209+ $ condition [] = "$ k = ' {$ this ->$ v }' " ;
210+ }
211+ try {
212+ $ relModel ->deleteAll (implode (" AND " , $ condition ));
213+ } catch (\yii \db \IntegrityException $ exc ) {
214+ $ this ->addError ($ relData [$ relName ]['name ' ], \Yii::t ('mtrelt ' , "Data can't be deleted because it's still used by another data. " ));
215+ $ error = true ;
216+ }
217+ } else {
218+ if ($ relData [$ relName ]['ismultiple ' ]) {
219+ foreach ($ relData [$ relName ]['link ' ] as $ k => $ v ) {
220+ $ condition [] = "$ k = ' {$ this ->$ v }' " ;
221+ }
222+ try {
223+ $ relModel ->deleteAll (implode (" AND " , $ condition ));
224+ } catch (\yii \db \IntegrityException $ exc ) {
225+ $ this ->addError ($ relData [$ relName ]['name ' ], \Yii::t ('mtrelt ' , "Data can't be deleted because it's still used by another data. " ));
226+ $ error = true ;
225227 }
226228 }
227229 }
@@ -258,7 +260,7 @@ public function deleteWithRelated()
258260 $ link = $ data ['link ' ];
259261 if (count ($ this ->{$ data ['name ' ]})) {
260262 $ relPKAttr = $ this ->{$ data ['name ' ]}[0 ]->primaryKey ();
261- $ isCompositePK = (count ($ relPKAttr ) > 1 );
263+ // $isCompositePK = (count($relPKAttr) > 1); // unused
262264 foreach ($ link as $ key => $ value ) {
263265 if (isset ($ this ->$ value )) {
264266 $ array [$ key ] = "$ key = ' {$ this ->$ value }' " ;
@@ -289,7 +291,6 @@ public function getRelationData()
289291 $ ARMethods = get_class_methods ('\yii\db\ActiveRecord ' );
290292 $ modelMethods = get_class_methods ('\yii\base\Model ' );
291293 $ reflection = new \ReflectionClass ($ this );
292- $ i = 0 ;
293294 $ stack = [];
294295 /* @var $method \ReflectionMethod */
295296 foreach ($ reflection ->getMethods () as $ method ) {
@@ -320,22 +321,22 @@ public function getRelationData()
320321 if ($ method ->name === 'deleteWithRelated ' ) {
321322 continue ;
322323 }
323- if (strpos ($ method ->name , 'get ' ) === false ) {
324+ if (strpos ($ method ->name , 'get ' ) === false ) {
324325 continue ;
325326 }
326327 try {
327328 $ rel = call_user_func (array ($ this , $ method ->name ));
328329 if ($ rel instanceof \yii \db \ActiveQuery) {
329- $ stack [ $ i ][ ' name ' ] = lcfirst (str_replace ('get ' , '' , $ method ->name ));
330- $ stack [$ i ]['method ' ] = $ method ->name ;
331- $ stack [$ i ]['ismultiple ' ] = $ rel -> multiple ;
332- $ stack [$ i ]['modelClass ' ] = $ rel ->modelClass ;
333- $ stack [$ i ]['link ' ] = $ rel ->link ;
334- $ stack [$ i ]['via ' ] = $ rel ->via ;
335- $ i ++ ;
330+ $ name = lcfirst (str_replace ('get ' , '' , $ method ->name ));
331+ $ stack [$ name ]['name ' ] = lcfirst ( str_replace ( ' get ' , '' , $ method ->name )) ;
332+ $ stack [$ name ]['method ' ] = $ method -> name ;
333+ $ stack [$ name ]['ismultiple ' ] = $ rel ->multiple ;
334+ $ stack [$ name ]['modelClass ' ] = $ rel ->modelClass ;
335+ $ stack [$ name ]['link ' ] = $ rel ->link ;
336+ $ stack [ $ name ][ ' via ' ] = $ rel -> via ;
336337 }
337338 } catch (\yii \base \ErrorException $ exc ) {
338- //if method name can't be call,
339+ //if method name can't be called,
339340 }
340341 }
341342 return $ stack ;
@@ -350,11 +351,11 @@ public function getAttributesWithRelatedAsPost()
350351 $ return [$ shortName ] = $ this ->attributes ;
351352 foreach ($ this ->relatedRecords as $ name => $ records ) {
352353 $ AQ = $ this ->getRelation ($ name );
353- if ($ AQ ->multiple ){
354+ if ($ AQ ->multiple ) {
354355 foreach ($ records as $ index => $ record ) {
355356 $ return [$ name ][$ index ] = $ record ->attributes ;
356357 }
357- }else {
358+ } else {
358359 $ return [$ name ] = $ records ->attributes ;
359360 }
360361
@@ -367,11 +368,11 @@ public function getAttributesWithRelated()
367368 $ return = $ this ->attributes ;
368369 foreach ($ this ->relatedRecords as $ name => $ records ) {
369370 $ AQ = $ this ->getRelation ($ name );
370- if ($ AQ ->multiple ){
371+ if ($ AQ ->multiple ) {
371372 foreach ($ records as $ index => $ record ) {
372373 $ return [$ name ][$ index ] = $ record ->attributes ;
373374 }
374- }else {
375+ } else {
375376 $ return [$ name ] = $ records ->attributes ;
376377 }
377378 }
0 commit comments