Browse Source

Changed useFormName property name to relationKeyName and added related constants

Updated Changelog
Updated Readme
tags/1.6.0
Alban Jubert 6 years ago
parent
commit
7970a50abb
  1. 11
      CHANGELOG.md
  2. 6
      README.md
  3. 45
      src/SaveRelationsBehavior.php
  4. 6
      tests/SaveRelationsBehaviorTest.php

11
CHANGELOG.md

@ -1,5 +1,12 @@
# Yii2 Active Record Save Relations Behavior Change Log
## [1.5.3] (unreleased)
### Fixed
- Bug #36: Fix an issue with HasMany relations with composite keys (thx @leandrogehlen)
### Added
- Enh #37: Add a `relationKeyName` property to determine the key used to load relations data.
## [1.5.2]
### Fixed
- Fix a regression in Has One saving introduced by #30 fix.
@ -17,7 +24,7 @@
## [1.5.0]
### Added
- Enh #5: Ability to automaticaly delete related records along with the main model
- Enh #5: Ability to automatically delete related records along with the main model
### Fixed
- Bug #30: HasOne relation saving issue (thx @phrakon)
@ -92,4 +99,4 @@
- A new related model instance was previously generated instead.
## [1.0.0] 2016-03-26
Initial release
Initial release

6
README.md

@ -277,3 +277,9 @@ $project->loadRelations(Yii::$app->request->post());
You can even further simplify the process by adding the `SaveRelationsTrait` to your model.
In that case, a call to the `load()` method will also automatically trigger a call to the `loadRelations()` method by using the same data, so you basically won't have to change your controllers.
The `relationKeyName` property can be used to decide how the relations data will be retrieved from the data parameter.
Possible constants values are:
* `SaveRelationsBehavior::RELATION_KEY_FORMNAME` (default): the key name will be compute using the model [`formName()`](https://www.yiiframework.com/doc/api/2.0/yii-base-model#formName()-detail) method
* `SaveRelationsBehavior::RELATION_KEY_MODELNAME`: the relation name as defined in the behavior declarations will be used

45
src/SaveRelationsBehavior.php

@ -26,8 +26,11 @@ use yii\helpers\VarDumper;
class SaveRelationsBehavior extends Behavior
{
const RELATION_KEY_FORMNAME = 'formName';
const RELATION_KEY_MODELNAME = 'modelName';
public $relations = [];
public $useFormName = true;
public $relationKeyName = self::RELATION_KEY_FORMNAME;
private $_relations = [];
private $_oldRelationValue = []; // Store initial relations value
@ -732,12 +735,13 @@ class SaveRelationsBehavior extends Behavior
/** @var BaseActiveRecord $owner */
$owner = $this->owner;
foreach ($this->_relations as $relationName) {
/** @var ActiveQuery $relation */
$relation = $owner->getRelation($relationName);
$modelClass = $relation->modelClass;
/** @var ActiveQuery $relationalModel */
$relationalModel = new $modelClass;
$keyName = $this->useFormName ? $relationalModel->formName() : $relationName;
// /** @var ActiveQuery $relation */
// $relation = $owner->getRelation($relationName);
// $modelClass = $relation->modelClass;
// /** @var ActiveQuery $relationalModel */
// $relationalModel = new $modelClass;
//$keyName = ($this->relationKeyName === self::RELATION_KEY_FORMNAME ? $relationalModel->formName() : $relationName);
$keyName = $this->_getRelationKeyName($relationName);
if (array_key_exists($keyName, $data)) {
$owner->{$relationName} = $data[$keyName];
}
@ -762,4 +766,31 @@ class SaveRelationsBehavior extends Behavior
}
}
/**
* @param $relationName
* @return mixed
* @throws InvalidConfigException
*/
private function _getRelationKeyName($relationName)
{
switch ($this->relationKeyName) {
case self::RELATION_KEY_MODELNAME:
$keyName = $relationName;
break;
case self::RELATION_KEY_FORMNAME:
/** @var BaseActiveRecord $owner */
$owner = $this->owner;
/** @var ActiveQuery $relation */
$relation = $owner->getRelation($relationName);
$modelClass = $relation->modelClass;
/** @var ActiveQuery $relationalModel */
$relationalModel = new $modelClass;
$keyName = $relationalModel->formName();
break;
default:
throw new InvalidConfigException('Unknown relation key name');
}
return $keyName;
}
}

6
tests/SaveRelationsBehaviorTest.php

@ -850,9 +850,9 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
]);
$company->attachBehavior('saveRelations', [
'class' => SaveRelationsBehavior::className(),
'relations' => ['users'],
'useFormName' => false
'class' => SaveRelationsBehavior::className(),
'relations' => ['users'],
'relationKeyName' => SaveRelationsBehavior::RELATION_KEY_MODELNAME
]);
$data = [

Loading…
Cancel
Save