Skip to content

Commit 7555123

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

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

RelationTrait.php

Lines changed: 12 additions & 12 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

@@ -155,18 +155,18 @@ public function saveAll()
155155
// new \yii\db\Expression('(1,939)')
156156
// ]]])->createCommand()->rawSql;
157157
$compiledFields = implode(", ", array_keys($fields));
158-
$compiledNotDeletedPK = ['and',$notDeletedFK];
158+
$compiledNotDeletedPK = ['and', $notDeletedFK];
159159
$notIn = ['not in', new \yii\db\Expression("($compiledFields)")];
160-
foreach($notDeletedPK as $value){
160+
foreach ($notDeletedPK as $value) {
161161
$v = [];
162-
foreach($fields as $key => $f){
162+
foreach ($fields as $key => $f) {
163163
$v[] = $value[$key];
164164
}
165-
$c = implode(',',$v);
165+
$c = implode(',', $v);
166166
$content[] = new \yii\db\Expression("($c)");
167167
}
168-
array_push($notIn,$content);
169-
array_push($compiledNotDeletedPK,$notIn);
168+
array_push($notIn, $content);
169+
array_push($compiledNotDeletedPK, $notIn);
170170
// $a = $relModel->findAll($compiledNotDeletedPK);
171171
// echo "Compiled Not Deleted PK :\n ";
172172
// print_r($compiledNotDeletedPK). "\n";
@@ -179,7 +179,7 @@ public function saveAll()
179179
// print_r($contoh);
180180
// echo $relModel->find()->where($compiledNotDeletedPK)->createCommand()->rawSql;
181181
$relModel->deleteAll($compiledNotDeletedPK);
182-
try{
182+
try {
183183
$relModel->deleteAll($compiledNotDeletedPK);
184184
} catch (\yii\db\IntegrityException $exc) {
185185
$this->addError($name, "Data can't be deleted because it's still used by another data.");
@@ -210,11 +210,11 @@ public function saveAll()
210210
$relData = $this->getRelationData();
211211
foreach ($relData as $rel) {
212212
/* @var $relModel ActiveRecord */
213-
if(empty($rel['via'])){
213+
if (empty($rel['via'])) {
214214
$relModel = new $rel['modelClass'];
215215
$condition = [];
216216
$isManyMany = count($relModel->primaryKey()) > 1;
217-
if($isManyMany){
217+
if ($isManyMany) {
218218
foreach ($rel['link'] as $k => $v) {
219219
$condition[] = $k . " = " . $this->$v;
220220
}
@@ -224,7 +224,7 @@ public function saveAll()
224224
$this->addError($rel['name'], "Data can't be deleted because it's still used by another data.");
225225
$error = 1;
226226
}
227-
}else{
227+
} else {
228228
foreach ($rel['link'] as $k => $v) {
229229
$condition[] = $k . " = " . $this->$v;
230230
}

0 commit comments

Comments
 (0)