From 279fdf0cdd14f8f1fc2a5f6e32e123bae54622df Mon Sep 17 00:00:00 2001 From: Alban Jubert Date: Sat, 17 Mar 2018 18:54:49 +0100 Subject: [PATCH] Refactoring --- README.md | 7 ++++++- src/SaveRelationsBehavior.php | 30 +++++++++++++++--------------- src/SaveRelationsTrait.php | 2 +- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 4effa85..a75da79 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,12 @@ Yii2 Active Record Save Relations Behavior ========================================== Automatically validate and save related Active Record models. -[![Latest Stable Version](https://poser.pugx.org/la-haute-societe/yii2-save-relations-behavior/v/stable)](https://packagist.org/packages/la-haute-societe/yii2-save-relations-behavior) [![Total Downloads](https://poser.pugx.org/la-haute-societe/yii2-save-relations-behavior/downloads)](https://packagist.org/packages/la-haute-societe/yii2-save-relations-behavior) [![Latest Unstable Version](https://poser.pugx.org/la-haute-societe/yii2-save-relations-behavior/v/unstable)](https://packagist.org/packages/la-haute-societe/yii2-save-relations-behavior) [![License](https://poser.pugx.org/la-haute-societe/yii2-save-relations-behavior/license)](https://packagist.org/packages/la-haute-societe/yii2-save-relations-behavior) +[![Latest Stable Version](https://poser.pugx.org/la-haute-societe/yii2-save-relations-behavior/v/stable)](https://packagist.org/packages/la-haute-societe/yii2-save-relations-behavior) +[![Total Downloads](https://poser.pugx.org/la-haute-societe/yii2-save-relations-behavior/downloads)](https://packagist.org/packages/la-haute-societe/yii2-save-relations-behavior) +[![Code Coverage](https://scrutinizer-ci.com/g/la-haute-societe/yii2-save-relations-behavior/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/la-haute-societe/yii2-save-relations-behavior/?branch=master) +[![Build Status](https://scrutinizer-ci.com/g/la-haute-societe/yii2-save-relations-behavior/badges/build.png?b=master)](https://scrutinizer-ci.com/g/la-haute-societe/yii2-save-relations-behavior/build-status/master) +[![Latest Unstable Version](https://poser.pugx.org/la-haute-societe/yii2-save-relations-behavior/v/unstable)](https://packagist.org/packages/la-haute-societe/yii2-save-relations-behavior) +[![License](https://poser.pugx.org/la-haute-societe/yii2-save-relations-behavior/license)](https://packagist.org/packages/la-haute-societe/yii2-save-relations-behavior) Features diff --git a/src/SaveRelationsBehavior.php b/src/SaveRelationsBehavior.php index 31f8260..c7eff8e 100644 --- a/src/SaveRelationsBehavior.php +++ b/src/SaveRelationsBehavior.php @@ -131,6 +131,21 @@ class SaveRelationsBehavior extends Behavior } /** + * Set the named single relation with the given value + * @param $name + * @param $value + */ + protected function setSingleRelation($name, $value) + { + $relation = $this->owner->getRelation($name); + if (!($value instanceof $relation->modelClass)) { + $value = $this->processModelAsArray($value, $relation); + } + $this->_newRelationValue[$name] = $value; + $this->owner->populateRelation($name, $value); + } + + /** * Set the named multiple relation with the given value * @param $name * @param $value @@ -218,21 +233,6 @@ class SaveRelationsBehavior extends Behavior } /** - * Set the named single relation with the given value - * @param $name - * @param $value - */ - protected function setSingleRelation($name, $value) - { - $relation = $this->owner->getRelation($name); - if (!($value instanceof $relation->modelClass)) { - $value = $this->processModelAsArray($value, $relation); - } - $this->_newRelationValue[$name] = $value; - $this->owner->populateRelation($name, $value); - } - - /** * Before the owner model validation, save related models. * For `hasOne()` relations, set the according foreign keys of the owner model to be able to validate it * @param ModelEvent $event diff --git a/src/SaveRelationsTrait.php b/src/SaveRelationsTrait.php index f1fbca0..1836eb1 100644 --- a/src/SaveRelationsTrait.php +++ b/src/SaveRelationsTrait.php @@ -8,7 +8,7 @@ trait SaveRelationsTrait public function load($data, $formName = null) { $loaded = parent::load($data, $formName); - if ($loaded) { + if ($loaded && method_exists($this, 'loadRelations')) { $this->loadRelations($data, $formName = null); } return $loaded;