Skip to content

Commit 18f5a10

Browse files
committed
Refactoring
1 parent de6a36b commit 18f5a10

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed

src/LinkManyBehavior.php

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -180,59 +180,45 @@ protected function prepareRelation($definition, $data)
180180
$owner = $this->owner;
181181
$name = $definition->name;
182182

183-
$relation = $owner->getRelation($name);
184183
$records = [];
184+
$relation = $owner->getRelation($name);
185185

186-
if ($owner->getIsNewRecord()) {
187-
foreach ($data[$name] as $item) {
188-
$modelClass = $relation->modelClass;
189-
$model = $relation->via ? $modelClass::findOne($item) : new $modelClass();
186+
$relateds = $owner->{$name};
187+
$references = $this->initReferences($relateds);
188+
$relatedData = !$relation->via ? $this->normalizeData($data[$name], $relation) : $data[$name];
190189

191-
if (!$relation->via) {
192-
$this->fillRelation($model, $item, $definition->formName);
193-
}
190+
foreach ($relatedData as $item) {
191+
$primaryKey = $this->extractPrimaryKey($item, $relation);
192+
$index = !$owner->getIsNewRecord() ? array_search($primaryKey, $references) : false;
194193

195-
if ($model !== null) {
196-
$this->_inserteds[$name][] = $model;
197-
$records[] = $model;
198-
}
194+
if ($index === false) {
195+
$modelClass = $relation->modelClass;
196+
$model = $relation->via ? $modelClass::findOne($item) : new $modelClass();
197+
} else {
198+
$model = $relateds[$index];
199199
}
200-
} else {
201-
$relateds = $owner->{$name};
202-
$references = $this->initReferences($relateds);
203-
$relatedData = !$relation->via ? $this->normalizeData($data[$name], $relation) : $data[$name];
204-
205-
foreach ($relatedData as $item) {
206-
$primaryKey = $this->extractPrimaryKey($item, $relation);
207200

208-
if (($index = array_search($primaryKey, $references)) === false) {
209-
$modelClass = $relation->modelClass;
210-
$model = $relation->via ? $modelClass::findOne($item) : new $modelClass();
211-
212-
if ($model !== null) {
213-
$this->_inserteds[$name][] = $model;
214-
}
201+
if ($model !== null) {
202+
if ($index === false) {
203+
$this->_inserteds[$name][] = $model;
215204
} else {
216-
$model = $relateds[$index];
217205
$this->_updateds[$name][] = $model;
218206
}
219207

220-
if ($model !== null) {
221-
if (!$relation->via) {
222-
$this->fillRelation($model, $item, $definition->formName);
223-
}
224-
225-
$records[] = $model;
208+
if (!$relation->via) {
209+
$this->fillRelation($model, $item, $definition->formName);
226210
}
211+
212+
$records[] = $model;
227213
}
214+
}
228215

229-
$references = $this->initReferences($records);
230-
foreach ($relateds as $i => $related) {
231-
$primaryKey = $this->normalizePrimaryKey($related->getPrimaryKey());
216+
$references = $this->initReferences($records);
217+
foreach ($relateds as $i => $related) {
218+
$primaryKey = $this->normalizePrimaryKey($related->getPrimaryKey());
232219

233-
if (array_search($primaryKey, $references) === false) {
234-
$this->_deleteds[$name][] = $relateds[$i];
235-
}
220+
if (array_search($primaryKey, $references) === false) {
221+
$this->_deleteds[$name][] = $relateds[$i];
236222
}
237223
}
238224

0 commit comments

Comments
 (0)