%PDF- <> %âãÏÓ endobj 2 0 obj <> endobj 3 0 obj <>/ExtGState<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/Annots[ 28 0 R 29 0 R] /MediaBox[ 0 0 595.5 842.25] /Contents 4 0 R/Group<>/Tabs/S>> endobj ºaâÚÎΞ-ÌE1ÍØÄ÷{òò2ÿ ÛÖ^ÔÀá TÎ{¦?§®¥kuµùÕ5sLOšuY>endobj 2 0 obj<>endobj 2 0 obj<>endobj 2 0 obj<>endobj 2 0 obj<> endobj 2 0 obj<>endobj 2 0 obj<>es 3 0 R>> endobj 2 0 obj<> ox[ 0.000000 0.000000 609.600000 935.600000]/Fi endobj 3 0 obj<> endobj 7 1 obj<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI]>>/Subtype/Form>> stream
<?php
namespace mdm\behaviors\ar;
use Yii;
use yii\db\BaseActiveRecord;
use yii\validators\Validator;
use yii\base\InvalidConfigException;
/**
* ExtendedBehavior
* One to one relation and treat related attribute as its own property
*
* @property \yii\db\BaseActiveRecord $owner
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class ExtendedBehavior extends \yii\base\Behavior
{
/**
* @var \yii\db\BaseActiveRecord
*/
private $_relation;
/**
* Relation model class name
* @var string
*/
public $relationClass;
/**
* Relation key for to Model
* @see [[\yii\db\BaseActiveRecord::hasOne()]]
* @var array
*/
public $relationKey;
/**
* @inheritdoc
*/
public function events()
{
return[
BaseActiveRecord::EVENT_AFTER_FIND => 'loadRelation',
BaseActiveRecord::EVENT_AFTER_INSERT => 'afterSave',
BaseActiveRecord::EVENT_AFTER_UPDATE => 'afterSave',
BaseActiveRecord::EVENT_AFTER_DELETE => 'afterDelete'
];
}
/**
* @inheritdoc
*/
public function attach($owner)
{
parent::attach($owner);
$this->loadRelation();
$validators = $owner->validators;
foreach ($this->_relation->rules() as $rule) {
if ($rule instanceof Validator) {
$validators->append($rule);
} elseif (is_array($rule) && isset($rule[0], $rule[1])) { // attributes, validator type
$validator = Validator::createValidator($rule[1], $owner, (array) $rule[0], array_slice($rule, 2));
$validators->append($validator);
} else {
throw new InvalidConfigException('Invalid validation rule: a rule must specify both attribute names and validator type.');
}
}
}
/**
* @inheritdoc
*/
public function canGetProperty($name, $checkVars = true)
{
return $this->_relation->hasAttribute($name) ||
$this->_relation->canGetProperty($name, $checkVars) ||
parent::canGetProperty($name, $checkVars);
}
/**
* @inheritdoc
*/
public function canSetProperty($name, $checkVars = true)
{
return $this->_relation->hasAttribute($name) ||
$this->_relation->canSetProperty($name, $checkVars) ||
parent::canSetProperty($name, $checkVars);
}
/**
* @inheritdoc
*/
public function __get($name)
{
if ($this->_relation->hasAttribute($name) || $this->_relation->canGetProperty($name)) {
return $this->_relation->$name;
} else {
return parent::__get($name);
}
}
/**
* @inheritdoc
*/
public function __set($name, $value)
{
if ($this->_relation->hasAttribute($name) || $this->_relation->canGetProperty($name)) {
$this->_relation->$name = $value;
} else {
parent::__set($name, $value);
}
}
/**
* @inheritdoc
*/
public function loadRelation()
{
if ($this->relationKey === null) {
$this->relationKey = [];
foreach ($this->owner->primaryKey() as $name) {
$this->relationKey[$name] = $name;
}
}
/* @var $class \yii\db\BaseActiveRecord */
$class = $this->relationClass;
if ($this->owner->isNewRecord) {
$this->_relation = Yii::createObject($class);
} else {
$conditions = [];
foreach ($this->relationKey as $from => $to) {
$conditions[$to] = $this->owner[$from];
}
if (($this->_relation = $class::findOne($conditions)) === null) {
$conditions['class'] = $class;
$this->_relation = Yii::createObject($conditions);
}
}
}
/**
* Execute after model saved
* @param \yii\base\Event $event
*/
public function afterSave($event)
{
foreach ($this->relationKey as $from => $to) {
$this->_relation[$to] = $this->owner[$from];
}
$this->_relation->save();
}
/**
* Execute after model deleted
* @param \yii\base\Event $event
*/
public function afterDelete($event)
{
$this->_relation->delete();
}
}