Skip to content

Commit 8edd534

Browse files
committed
fix delete related many-many when no relation left
TODO : add feature for has one relation
1 parent 7555123 commit 8edd534

File tree

1 file changed

+12
-48
lines changed

1 file changed

+12
-48
lines changed

RelationTrait.php

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ public function saveAll()
9999
$notDeletedFK = [];
100100
foreach ($link as $key => $value) {
101101
$relModel->$key = $this->$value;
102-
if ($isManyMany) $notDeletedFK[$key] = $this->$value;
103-
elseif ($isHasMany) $notDeletedFK[$key] = "$key = '{$this->$value}'";
102+
if($isManyMany) $notDeletedFK[$key] = $this->$value;
103+
elseif($isHasMany) $notDeletedFK[$key] = "$key = '{$this->$value}'";
104104
}
105105
$relSave = $relModel->save();
106106

@@ -129,57 +129,21 @@ public function saveAll()
129129
if (!$this->isNewRecord) {
130130
//DELETE WITH 'NOT IN' PK MODEL & REL MODEL
131131
if ($isManyMany) {
132-
// echo "composite pk\n";
133-
// echo "not deleted PK : \n";
134-
// print_r($notDeletedPK);
135-
// echo "not deleted FK : \n";
136-
// print_r($notDeletedFK);
137-
// echo "\nfields : \n";
138-
// 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-
// ]]];
148-
// echo $relModel::find()->where(['and',['actor_id' => 1],['not in', new \yii\db\Expression('(actor_id, film_id)'),
149-
// [
150-
// new \yii\db\Expression('(1,1)'),
151-
// new \yii\db\Expression('(1,23)'),
152-
// new \yii\db\Expression('(1,25)'),
153-
// new \yii\db\Expression('(1,980)'),
154-
// new \yii\db\Expression('(1,970)'),
155-
// new \yii\db\Expression('(1,939)')
156-
// ]]])->createCommand()->rawSql;
157132
$compiledFields = implode(", ", array_keys($fields));
158-
$compiledNotDeletedPK = ['and', $notDeletedFK];
133+
$compiledNotDeletedPK = ['and',$notDeletedFK];
159134
$notIn = ['not in', new \yii\db\Expression("($compiledFields)")];
160-
foreach ($notDeletedPK as $value) {
135+
foreach($notDeletedPK as $value){
161136
$v = [];
162-
foreach ($fields as $key => $f) {
137+
foreach($fields as $key => $f){
163138
$v[] = $value[$key];
164139
}
165-
$c = implode(',', $v);
140+
$c = implode(',',$v);
166141
$content[] = new \yii\db\Expression("($c)");
167142
}
168-
array_push($notIn, $content);
169-
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;
143+
array_push($notIn,$content);
144+
array_push($compiledNotDeletedPK,$notIn);
181145
$relModel->deleteAll($compiledNotDeletedPK);
182-
try {
146+
try{
183147
$relModel->deleteAll($compiledNotDeletedPK);
184148
} catch (\yii\db\IntegrityException $exc) {
185149
$this->addError($name, "Data can't be deleted because it's still used by another data.");
@@ -210,11 +174,11 @@ public function saveAll()
210174
$relData = $this->getRelationData();
211175
foreach ($relData as $rel) {
212176
/* @var $relModel ActiveRecord */
213-
if (empty($rel['via'])) {
177+
if(empty($rel['via'])){
214178
$relModel = new $rel['modelClass'];
215179
$condition = [];
216180
$isManyMany = count($relModel->primaryKey()) > 1;
217-
if ($isManyMany) {
181+
if($isManyMany){
218182
foreach ($rel['link'] as $k => $v) {
219183
$condition[] = $k . " = " . $this->$v;
220184
}
@@ -224,7 +188,7 @@ public function saveAll()
224188
$this->addError($rel['name'], "Data can't be deleted because it's still used by another data.");
225189
$error = 1;
226190
}
227-
} else {
191+
}else{
228192
foreach ($rel['link'] as $k => $v) {
229193
$condition[] = $k . " = " . $this->$v;
230194
}

0 commit comments

Comments
 (0)