Browse Source

Small refactoring

tags/1.5.1
Alban Jubert 6 years ago
parent
commit
4ef6c6099f
  1. 21
      src/SaveRelationsBehavior.php

21
src/SaveRelationsBehavior.php

@ -380,8 +380,6 @@ class SaveRelationsBehavior extends Behavior
*/ */
private function _prepareHasOneRelation(BaseActiveRecord $model, $relationName, ModelEvent $event) private function _prepareHasOneRelation(BaseActiveRecord $model, $relationName, ModelEvent $event)
{ {
/** @var ActiveQuery $relation */
$relation = $model->getRelation($relationName);
$relationModel = $model->{$relationName}; $relationModel = $model->{$relationName};
$this->validateRelationModel(self::prettyRelationName($relationName), $relationName, $model->{$relationName}); $this->validateRelationModel(self::prettyRelationName($relationName), $relationName, $model->{$relationName});
if ($relationModel->getIsNewRecord()) { if ($relationModel->getIsNewRecord()) {
@ -663,15 +661,16 @@ class SaveRelationsBehavior extends Behavior
*/ */
public function beforeDelete() public function beforeDelete()
{ {
$model = $this->owner; /** @var BaseActiveRecord $owner */
$owner = $this->owner;
foreach ($this->_relationsCascadeDelete as $relationName => $params) { foreach ($this->_relationsCascadeDelete as $relationName => $params) {
if ($params === true) { if ($params === true) {
$relation = $model->getRelation($relationName); $relation = $owner->getRelation($relationName);
if (!empty($model->{$relationName})) { if (!empty($owner->{$relationName})) {
if ($relation->multiple === true) { // Has many relation if ($relation->multiple === true) { // Has many relation
$this->_relationsToDelete = ArrayHelper::merge($this->_relationsToDelete, $model->{$relationName}); $this->_relationsToDelete = ArrayHelper::merge($this->_relationsToDelete, $owner->{$relationName});
} else { } else {
$this->_relationsToDelete[] = $model->{$relationName}; $this->_relationsToDelete[] = $owner->{$relationName};
} }
} }
} }
@ -704,17 +703,17 @@ class SaveRelationsBehavior extends Behavior
*/ */
public function loadRelations($data) public function loadRelations($data)
{ {
/** @var BaseActiveRecord $model */ /** @var BaseActiveRecord $owner */
$model = $this->owner; $owner = $this->owner;
foreach ($this->_relations as $relationName) { foreach ($this->_relations as $relationName) {
/** @var ActiveQuery $relation */ /** @var ActiveQuery $relation */
$relation = $model->getRelation($relationName); $relation = $owner->getRelation($relationName);
$modelClass = $relation->modelClass; $modelClass = $relation->modelClass;
/** @var ActiveQuery $relationalModel */ /** @var ActiveQuery $relationalModel */
$relationalModel = new $modelClass; $relationalModel = new $modelClass;
$formName = $relationalModel->formName(); $formName = $relationalModel->formName();
if (array_key_exists($formName, $data)) { if (array_key_exists($formName, $data)) {
$model->{$relationName} = $data[$formName]; $owner->{$relationName} = $data[$formName];
} }
} }
} }

Loading…
Cancel
Save