@@ -136,15 +136,15 @@ public function saveAll()
136136// print_r($notDeletedFK);
137137// echo "\nfields : \n";
138138// print_r($fields) . "\n";
139- $ contoh = ['and ' ,['actor_id ' => 1 ],['not in ' , new \yii \db \Expression ('(actor_id, film_id) ' ),
140- [
141- new \yii \db \Expression ('(1,1) ' ),
142- new \yii \db \Expression ('(1,23) ' ),
143- new \yii \db \Expression ('(1,25) ' ),
144- new \yii \db \Expression ('(1,980) ' ),
145- new \yii \db \Expression ('(1,970) ' ),
146- new \yii \db \Expression ('(1,939) ' )
147- ]]];
139+ // $contoh = ['and',['actor_id' => 1],['not in', new \yii\db\Expression('(actor_id, film_id)'),
140+ // [
141+ // new \yii\db\Expression('(1,1)'),
142+ // new \yii\db\Expression('(1,23)'),
143+ // new \yii\db\Expression('(1,25)'),
144+ // new \yii\db\Expression('(1,980)'),
145+ // new \yii\db\Expression('(1,970)'),
146+ // new \yii\db\Expression('(1,939)')
147+ // ]]];
148148// echo $relModel::find()->where(['and',['actor_id' => 1],['not in', new \yii\db\Expression('(actor_id, film_id)'),
149149// [
150150// new \yii\db\Expression('(1,1)'),
@@ -167,6 +167,18 @@ public function saveAll()
167167 }
168168 array_push ($ notIn ,$ content );
169169 array_push ($ compiledNotDeletedPK ,$ notIn );
170+ // $a = $relModel->findAll($compiledNotDeletedPK);
171+ // echo "Compiled Not Deleted PK :\n ";
172+ // print_r($compiledNotDeletedPK). "\n";
173+ // print_r($a);
174+ // foreach($a as $ac){
175+ // print_r($ac->attributes);
176+ // }
177+ // print_r($compiledNotDeletedPK) . "\n";
178+ // echo "Contoh :\n ";
179+ // print_r($contoh);
180+ // echo $relModel->find()->where($compiledNotDeletedPK)->createCommand()->rawSql;
181+ $ relModel ->deleteAll ($ compiledNotDeletedPK );
170182 try {
171183 $ relModel ->deleteAll ($ compiledNotDeletedPK );
172184 } catch (\yii \db \IntegrityException $ exc ) {
@@ -193,21 +205,36 @@ public function saveAll()
193205 }
194206 } else {
195207 //No Children left
208+ echo "No Children left " ;
196209 if (!$ this ->isNewRecord ) {
197210 $ relData = $ this ->getRelationData ();
198211 foreach ($ relData as $ rel ) {
199212 /* @var $relModel ActiveRecord */
200- $ relModel = new $ rel ['modelClass ' ];
201- $ condition = [];
202- foreach ($ rel ['link ' ] as $ k => $ v ) {
203- if (property_exists ($ this , $ v ))
204- $ condition [] = $ k . " = " . $ this ->$ v ;
205- }
206- try {
207- $ relModel ->deleteAll (implode (" AND " , $ condition ));
208- } catch (\yii \db \IntegrityException $ exc ) {
209- $ this ->addError ($ rel ['name ' ], "Data can't be deleted because it's still used by another data. " );
210- $ error = 1 ;
213+ if (empty ($ rel ['via ' ])){
214+ $ relModel = new $ rel ['modelClass ' ];
215+ $ condition = [];
216+ $ isManyMany = count ($ relModel ->primaryKey ()) > 1 ;
217+ if ($ isManyMany ){
218+ foreach ($ rel ['link ' ] as $ k => $ v ) {
219+ $ condition [] = $ k . " = " . $ this ->$ v ;
220+ }
221+ try {
222+ $ relModel ->deleteAll (implode (" AND " , $ condition ));
223+ } catch (\yii \db \IntegrityException $ exc ) {
224+ $ this ->addError ($ rel ['name ' ], "Data can't be deleted because it's still used by another data. " );
225+ $ error = 1 ;
226+ }
227+ }else {
228+ foreach ($ rel ['link ' ] as $ k => $ v ) {
229+ $ condition [] = $ k . " = " . $ this ->$ v ;
230+ }
231+ try {
232+ $ relModel ->deleteAll (implode (" AND " , $ condition ));
233+ } catch (\yii \db \IntegrityException $ exc ) {
234+ $ this ->addError ($ rel ['name ' ], "Data can't be deleted because it's still used by another data. " );
235+ $ error = 1 ;
236+ }
237+ }
211238 }
212239 }
213240 }
@@ -311,6 +338,7 @@ public function getRelationData()
311338 $ stack [$ i ]['ismultiple ' ] = $ rel ->multiple ;
312339 $ stack [$ i ]['modelClass ' ] = $ rel ->modelClass ;
313340 $ stack [$ i ]['link ' ] = $ rel ->link ;
341+ $ stack [$ i ]['via ' ] = $ rel ->via ;
314342 $ i ++;
315343 }
316344 } catch (\yii \base \ErrorException $ exc ) {
0 commit comments