Skip to content

Commit d568c4c

Browse files
committed
many-many support
Change string sql to array for QueryBuilder to fix quote string with different database
1 parent 9585b2f commit d568c4c

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

RelationTrait.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function loadAll($POST, $skippedRelations = [])
3232
$isHasMany = is_array($value) && is_array(current($value));
3333
$relName = ($isHasMany) ? lcfirst(Inflector::pluralize($key)) : lcfirst($key);
3434

35-
if (in_array($relName, $skippedRelations) || !array_key_exists($relName,$relData)){
35+
if (in_array($relName, $skippedRelations) || !array_key_exists($relName, $relData)) {
3636
continue;
3737
}
3838

@@ -130,8 +130,11 @@ public function saveAll($skippedRelations = [])
130130
} else {
131131
//GET PK OF REL MODEL
132132
if ($isManyMany) {
133+
$mainPK = array_keys($link)[0];
133134
foreach ($relModel->primaryKey as $attr => $value) {
134-
$notDeletedPK[$attr][] = $value;
135+
if ($attr != $mainPK) {
136+
$notDeletedPK[$attr][] = $value;
137+
}
135138
}
136139
} else {
137140
$notDeletedPK[] = $relModel->primaryKey;
@@ -142,9 +145,13 @@ public function saveAll($skippedRelations = [])
142145
//DELETE WITH 'NOT IN' PK MODEL & REL MODEL
143146
if ($isManyMany) {
144147
// Many Many
145-
$notIn = ['not in', $notDeletedPK];
148+
$query = ['and', $notDeletedFK];
149+
foreach ($notDeletedPK as $attr => $value) {
150+
$notIn = ['not in', $attr, $value];
151+
array_push($query, $notIn);
152+
}
146153
try {
147-
$relModel->deleteAll(['and', $notDeletedFK,$notIn]);
154+
$relModel->deleteAll($query);
148155
} catch (\yii\db\IntegrityException $exc) {
149156
$this->addError($name, "Data can't be deleted because it's still used by another data.");
150157
$error = true;
@@ -180,7 +187,7 @@ public function saveAll($skippedRelations = [])
180187
}
181188
}
182189
}
183-
}else{
190+
} else {
184191
//No Children left
185192
$relAvail = array_keys($this->relatedRecords);
186193
$relData = $this->getRelationData();
@@ -260,7 +267,7 @@ public function deleteWithRelated($skippedRelations = [])
260267
$array[$key] = $this->$value;
261268
}
262269
}
263-
$error = !$this->{$data['name']}[0]->deleteAll(['and', $array]);
270+
// $error = !$this->{$data['name']}[0]->deleteAll(['and', $array]);
264271
}
265272
}
266273
}

0 commit comments

Comments
 (0)