Browse Source

More meaningful constants names for relations key name

tags/1.6.0
Alban Jubert 6 years ago
parent
commit
632b8298ab
  1. 4
      README.md
  2. 10
      src/SaveRelationsBehavior.php
  3. 2
      tests/SaveRelationsBehaviorTest.php

4
README.md

@ -281,5 +281,5 @@ In that case, a call to the `load()` method will also automatically trigger a ca
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
* `SaveRelationsBehavior::RELATION_KEY_FORM_NAME` (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_RELATION_NAME`: the relation name as defined in the behavior declarations will be used

10
src/SaveRelationsBehavior.php

@ -27,11 +27,11 @@ use yii\helpers\VarDumper;
class SaveRelationsBehavior extends Behavior
{
const RELATION_KEY_FORMNAME = 'formName';
const RELATION_KEY_MODELNAME = 'modelName';
const RELATION_KEY_FORM_NAME = 'formName';
const RELATION_KEY_RELATION_NAME = 'relationName';
public $relations = [];
public $relationKeyName = self::RELATION_KEY_FORMNAME;
public $relationKeyName = self::RELATION_KEY_FORM_NAME;
private $_relations = [];
private $_oldRelationValue = []; // Store initial relations value
@ -776,10 +776,10 @@ class SaveRelationsBehavior extends Behavior
private function _getRelationKeyName($relationName)
{
switch ($this->relationKeyName) {
case self::RELATION_KEY_MODELNAME:
case self::RELATION_KEY_RELATION_NAME:
$keyName = $relationName;
break;
case self::RELATION_KEY_FORMNAME:
case self::RELATION_KEY_FORM_NAME:
/** @var BaseActiveRecord $owner */
$owner = $this->owner;
/** @var ActiveQuery $relation */

2
tests/SaveRelationsBehaviorTest.php

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

Loading…
Cancel
Save