Browse Source

Refactoring

tags/1.5.0
Alban Jubert 7 years ago
parent
commit
279fdf0cdd
  1. 7
      README.md
  2. 30
      src/SaveRelationsBehavior.php
  3. 2
      src/SaveRelationsTrait.php

7
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

30
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

2
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;

Loading…
Cancel
Save