Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Enh: Аdjust access to save the relation #66 checkRelationsSafe #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/SaveRelationsBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class SaveRelationsBehavior extends Behavior

public $relations = [];
public $relationKeyName = self::RELATION_KEY_FORM_NAME;
/**
* Save only safe relation. Check as attribute.
* @see https://www.yiiframework.com/doc/guide/2.0/en/input-validation
* @var bool
*/
public $checkRelationsSafe = false;

private $_relations = [];
private $_oldRelationValue = []; // Store initial relations value
Expand Down Expand Up @@ -729,6 +735,7 @@ public function loadRelations($data)
/** @var BaseActiveRecord $owner */
$owner = $this->owner;
foreach ($this->_relations as $relationName) {
if($this->checkRelationsSafe && !$owner->isAttributeSafe($relationName)) continue;
$keyName = $this->_getRelationKeyName($relationName);
if (array_key_exists($keyName, $data)) {
$owner->{$relationName} = $data[$keyName];
Expand Down