|
|
@ -5,6 +5,7 @@ namespace lhs\Yii2SaveRelationsBehavior; |
|
|
|
use RuntimeException; |
|
|
|
use RuntimeException; |
|
|
|
use Yii; |
|
|
|
use Yii; |
|
|
|
use yii\base\Behavior; |
|
|
|
use yii\base\Behavior; |
|
|
|
|
|
|
|
use yii\base\Component; |
|
|
|
use yii\base\Exception; |
|
|
|
use yii\base\Exception; |
|
|
|
use yii\base\ModelEvent; |
|
|
|
use yii\base\ModelEvent; |
|
|
|
use yii\base\UnknownPropertyException; |
|
|
|
use yii\base\UnknownPropertyException; |
|
|
@ -75,7 +76,7 @@ class SaveRelationsBehavior extends Behavior |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Check if the behavior is attached to an Active Record |
|
|
|
* Check if the behavior is attached to an Active Record |
|
|
|
* @param BaseActiveRecord $owner |
|
|
|
* @param Component $owner |
|
|
|
* @throws RuntimeException |
|
|
|
* @throws RuntimeException |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function attach($owner) |
|
|
|
public function attach($owner) |
|
|
@ -95,7 +96,9 @@ class SaveRelationsBehavior extends Behavior |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function canSetProperty($name, $checkVars = true) |
|
|
|
public function canSetProperty($name, $checkVars = true) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (in_array($name, $this->_relations) && $this->owner->getRelation($name, false)) { |
|
|
|
/** @var BaseActiveRecord $owner */ |
|
|
|
|
|
|
|
$owner = $this->owner; |
|
|
|
|
|
|
|
if (in_array($name, $this->_relations) && $owner->getRelation($name, false)) { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
return parent::canSetProperty($name, $checkVars); |
|
|
|
return parent::canSetProperty($name, $checkVars); |
|
|
@ -106,23 +109,26 @@ class SaveRelationsBehavior extends Behavior |
|
|
|
* a primary key value or an associative array |
|
|
|
* a primary key value or an associative array |
|
|
|
* @param string $name |
|
|
|
* @param string $name |
|
|
|
* @param mixed $value |
|
|
|
* @param mixed $value |
|
|
|
|
|
|
|
* @throws \yii\base\InvalidArgumentException |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function __set($name, $value) |
|
|
|
public function __set($name, $value) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
/** @var BaseActiveRecord $owner */ |
|
|
|
|
|
|
|
$owner = $this->owner; |
|
|
|
if (in_array($name, $this->_relations)) { |
|
|
|
if (in_array($name, $this->_relations)) { |
|
|
|
Yii::debug("Setting {$name} relation value", __METHOD__); |
|
|
|
Yii::debug("Setting {$name} relation value", __METHOD__); |
|
|
|
if (!isset($this->_oldRelationValue[$name])) { |
|
|
|
if (!isset($this->_oldRelationValue[$name])) { |
|
|
|
if ($this->owner->isNewRecord) { |
|
|
|
if ($owner->isNewRecord) { |
|
|
|
if ($this->owner->getRelation($name)->multiple === true) { |
|
|
|
if ($owner->getRelation($name)->multiple === true) { |
|
|
|
$this->_oldRelationValue[$name] = []; |
|
|
|
$this->_oldRelationValue[$name] = []; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$this->_oldRelationValue[$name] = null; |
|
|
|
$this->_oldRelationValue[$name] = null; |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$this->_oldRelationValue[$name] = $this->owner->{$name}; |
|
|
|
$this->_oldRelationValue[$name] = $owner->{$name}; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if ($this->owner->getRelation($name)->multiple === true) { |
|
|
|
if ($owner->getRelation($name)->multiple === true) { |
|
|
|
$this->setMultipleRelation($name, $value); |
|
|
|
$this->setMultipleRelation($name, $value); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$this->setSingleRelation($name, $value); |
|
|
|
$this->setSingleRelation($name, $value); |
|
|
@ -134,25 +140,31 @@ class SaveRelationsBehavior extends Behavior |
|
|
|
* Set the named single relation with the given value |
|
|
|
* Set the named single relation with the given value |
|
|
|
* @param $name |
|
|
|
* @param $name |
|
|
|
* @param $value |
|
|
|
* @param $value |
|
|
|
|
|
|
|
* @throws \yii\base\InvalidArgumentException |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function setSingleRelation($name, $value) |
|
|
|
protected function setSingleRelation($name, $value) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$relation = $this->owner->getRelation($name); |
|
|
|
/** @var BaseActiveRecord $owner */ |
|
|
|
|
|
|
|
$owner = $this->owner; |
|
|
|
|
|
|
|
$relation = $owner->getRelation($name); |
|
|
|
if (!($value instanceof $relation->modelClass)) { |
|
|
|
if (!($value instanceof $relation->modelClass)) { |
|
|
|
$value = $this->processModelAsArray($value, $relation); |
|
|
|
$value = $this->processModelAsArray($value, $relation); |
|
|
|
} |
|
|
|
} |
|
|
|
$this->_newRelationValue[$name] = $value; |
|
|
|
$this->_newRelationValue[$name] = $value; |
|
|
|
$this->owner->populateRelation($name, $value); |
|
|
|
$owner->populateRelation($name, $value); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Set the named multiple relation with the given value |
|
|
|
* Set the named multiple relation with the given value |
|
|
|
* @param $name |
|
|
|
* @param $name |
|
|
|
* @param $value |
|
|
|
* @param $value |
|
|
|
|
|
|
|
* @throws \yii\base\InvalidArgumentException |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function setMultipleRelation($name, $value) |
|
|
|
protected function setMultipleRelation($name, $value) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$relation = $this->owner->getRelation($name); |
|
|
|
/** @var BaseActiveRecord $owner */ |
|
|
|
|
|
|
|
$owner = $this->owner; |
|
|
|
|
|
|
|
$relation = $owner->getRelation($name); |
|
|
|
$newRelations = []; |
|
|
|
$newRelations = []; |
|
|
|
if (!is_array($value)) { |
|
|
|
if (!is_array($value)) { |
|
|
|
if (!empty($value)) { |
|
|
|
if (!empty($value)) { |
|
|
@ -170,7 +182,7 @@ class SaveRelationsBehavior extends Behavior |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
$this->_newRelationValue[$name] = $newRelations; |
|
|
|
$this->_newRelationValue[$name] = $newRelations; |
|
|
|
$this->owner->populateRelation($name, $newRelations); |
|
|
|
$owner->populateRelation($name, $newRelations); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|