Skip to content

Commit 952811e

Browse files
committed
Update RelationTrait.php
if(!empty($notDeletedPK) && !empty($notDeletedFK)) on line 98
1 parent 077c6d5 commit 952811e

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

RelationTrait.php

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -54,50 +54,52 @@ public function saveAll() {
5454
try {
5555
if ($this->save()) {
5656
$error = 0;
57-
foreach ($this->relatedRecords as $name => $records) {
58-
$AQ = $this->getRelation($name);
59-
$link = $AQ->link;
60-
$notDeletedPK = [];
61-
$relPKAttr = $records[0]->primaryKey();
62-
$isCompositePK = (count($relPKAttr) > 1);
63-
/* @var $relModel ActiveRecord */
64-
foreach($records as $index => $relModel){
65-
foreach ($link as $key => $value){
66-
$relModel->$key = $this->$value;
67-
$notDeletedFK[$key] = "$key = '{$this->$value}'";
68-
}
69-
if(!$relModel->save()){
70-
$relModelWords = Inflector::camel2words(StringHelper::basename($AQ->modelClass));
71-
$index++;
72-
foreach ($relModel->errors as $validation){
73-
foreach($validation as $errorMsg){
74-
$this->addError($name,"$relModelWords #$index : $errorMsg");
75-
}
57+
if (!empty($this->relatedRecords)) {
58+
foreach ($this->relatedRecords as $name => $records) {
59+
$AQ = $this->getRelation($name);
60+
$link = $AQ->link;
61+
$notDeletedPK = [];
62+
$relPKAttr = $records[0]->primaryKey();
63+
$isCompositePK = (count($relPKAttr) > 1);
64+
/* @var $relModel ActiveRecord */
65+
foreach($records as $index => $relModel){
66+
foreach ($link as $key => $value){
67+
$relModel->$key = $this->$value;
68+
$notDeletedFK[$key] = "$key = '{$this->$value}'";
7669
}
77-
$error = 1;
78-
}else{
79-
//GET PK OF REL MODEL
80-
if($isCompositePK){
81-
foreach($relModel->primaryKey as $attr => $value){
82-
$notDeletedPK[$attr][] = "'$value'";
70+
if(!$relModel->save()){
71+
$relModelWords = Inflector::camel2words(StringHelper::basename($AQ->modelClass));
72+
$index++;
73+
foreach ($relModel->errors as $validation){
74+
foreach($validation as $errorMsg){
75+
$this->addError($name,"$relModelWords #$index : $errorMsg");
76+
}
77+
}
78+
$error = 1;
79+
}else{
80+
//GET PK OF REL MODEL
81+
if($isCompositePK){
82+
foreach($relModel->primaryKey as $attr => $value){
83+
$notDeletedPK[$attr][] = "'$value'";
84+
}
85+
} else {
86+
$notDeletedPK[] = "'$relModel->primaryKey'";
8387
}
84-
} else {
85-
$notDeletedPK[] = "'$relModel->primaryKey'";
8688
}
8789
}
88-
}
89-
if(!$this->isNewRecord){
90-
//DELETE WITH 'NOT IN' PK MODEL & REL MODEL
91-
$notDeletedFK = implode(' AND ', $notDeletedFK);
92-
if($isCompositePK){
93-
$compiledNotDeletedPK = [];
94-
foreach($notDeletedPK as $attr => $pks){
95-
$compiledNotDeletedPK[$attr] = "$attr NOT IN(".implode(', ', $pks).")";
96-
$relModel->deleteAll("$notDeletedFK AND ".implode(' AND ', $compiledNotDeletedPK));
90+
if(!empty($notDeletedPK) && !empty($notDeletedFK)){
91+
//DELETE WITH 'NOT IN' PK MODEL & REL MODEL
92+
$notDeletedFK = implode(' AND ', $notDeletedFK);
93+
if($isCompositePK){
94+
$compiledNotDeletedPK = [];
95+
foreach($notDeletedPK as $attr => $pks){
96+
$compiledNotDeletedPK[$attr] = "$attr NOT IN(".implode(', ', $pks).")";
97+
$relModel->deleteAll("$notDeletedFK AND ".implode(' AND ', $compiledNotDeletedPK));
98+
}
99+
}else{
100+
$compiledNotDeletedPK = implode(',', $notDeletedPK);
101+
$relModel->deleteAll($notDeletedFK.' AND '.$relPKAttr[0]." NOT IN ($compiledNotDeletedPK)");
97102
}
98-
}else{
99-
$compiledNotDeletedPK = implode(',', $notDeletedPK);
100-
$relModel->deleteAll($notDeletedFK.' AND '.$relPKAttr[0]." NOT IN ($compiledNotDeletedPK)");
101103
}
102104
}
103105
}

0 commit comments

Comments
 (0)