Browse Source

Refactoring

tags/1.5.0
Alban Jubert 7 years ago
parent
commit
aef5f41a2e
  1. 17
      src/SaveRelationsBehavior.php

17
src/SaveRelationsBehavior.php

@ -337,7 +337,7 @@ class SaveRelationsBehavior extends Behavior
*/
protected function saveModelRecord(BaseActiveRecord $model, ModelEvent $event, $pettyRelationName, $relationName)
{
$this->validateRelationModel($pettyRelationName, $relationName, $model, $event);
$this->validateRelationModel($pettyRelationName, $relationName, $model);
if ($event->isValid && (count($model->dirtyAttributes) || $model->isNewRecord)) {
Yii::debug("Saving {$pettyRelationName} relation model", __METHOD__);
$model->save(false);
@ -523,7 +523,6 @@ class SaveRelationsBehavior extends Behavior
$owner = $this->owner;
$relation = $owner->getRelation($relationName);
if ($this->_hasActiveRelationTrait($relation)) {
// Process new relations
$existingRecords = [];
/** @var ActiveQuery $relationModel */
@ -583,7 +582,6 @@ class SaveRelationsBehavior extends Behavior
$owner->link($relationName, $actualModels[$key], $junctionTableColumns);
}
}
}
/**
* @param $relationName
@ -591,9 +589,7 @@ class SaveRelationsBehavior extends Behavior
private function _afterSaveHasOneRelation($relationName)
{
$owner = $this->owner;
$relation = $owner->getRelation($relationName);
if ($this->_hasActiveRelationTrait($relation)) {
if ($this->_oldRelationValue[$relationName] !== $owner->{$relationName}) {
if ($owner->{$relationName} instanceof BaseActiveRecord) {
$owner->link($relationName, $owner->{$relationName});
@ -607,15 +603,4 @@ class SaveRelationsBehavior extends Behavior
$owner->{$relationName}->save();
}
}
}
/**
* @param ActiveQuery $relation
* @return bool
*/
private function _hasActiveRelationTrait(ActiveQuery $relation)
{
$relationTraits = class_uses($relation);
return in_array('yii\db\ActiveRelationTrait', $relationTraits);
}
}

Loading…
Cancel
Save