Skip to content

Commit a6b848d

Browse files
committed
fixed behavior on collection update
1 parent e341e20 commit a6b848d

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

src/model/ActiveRecord.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,26 @@
44

55
use yii\helpers\ArrayHelper;
66
use yii\db\ActiveRecord as BaseModel;
7+
use indigerd\embedded\behavior\HydrateCollectionBehavior;
78

89
class ActiveRecord extends BaseModel
910
{
1011
public function setAttributes($values, $safeOnly = true)
1112
{
1213
$this->trigger(Model::EVENT_BEFORE_POPULATE);
13-
$oldValues = $this->getAttributes(\array_keys($values));
14+
$keys = [];
15+
foreach ($values as $key => $value) {
16+
if ($this->hasAttribute($key)) {
17+
$keys[] = $key;
18+
}
19+
}
20+
$oldValues = $this->getAttributes($keys);
21+
$behaviors = $this->getBehaviors();
22+
foreach ($behaviors as $behavior) {
23+
if ($behavior instanceof HydrateCollectionBehavior and isset($values[$behavior->attribute])) {
24+
unset($oldValues[$behavior->attribute]);
25+
}
26+
}
1427
$values = ArrayHelper::merge($oldValues, $values);
1528
parent::setAttributes($values, $safeOnly);
1629
$this->trigger(Model::EVENT_AFTER_POPULATE);

src/model/Model.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use yii\helpers\ArrayHelper;
66
use yii\base\Model as BaseModel;
7+
use indigerd\embedded\behavior\HydrateCollectionBehavior;
78

89
class Model extends BaseModel
910
{
@@ -12,10 +13,23 @@ class Model extends BaseModel
1213

1314
public function setAttributes($values, $safeOnly = true)
1415
{
15-
$this->trigger(self::EVENT_BEFORE_POPULATE);
16-
$oldValues = $this->getAttributes(\array_keys($values));
16+
$this->trigger(Model::EVENT_BEFORE_POPULATE);
17+
$keys = [];
18+
$reflection = new \ReflectionClass($this);
19+
foreach ($values as $key => $value) {
20+
if ($reflection->hasProperty($key)) {
21+
$keys[] = $key;
22+
}
23+
}
24+
$oldValues = $this->getAttributes($keys);
25+
$behaviors = $this->getBehaviors();
26+
foreach ($behaviors as $behavior) {
27+
if ($behavior instanceof HydrateCollectionBehavior and isset($values[$behavior->attribute])) {
28+
unset($oldValues[$behavior->attribute]);
29+
}
30+
}
1731
$values = ArrayHelper::merge($oldValues, $values);
1832
parent::setAttributes($values, $safeOnly);
19-
$this->trigger(self::EVENT_AFTER_POPULATE);
33+
$this->trigger(Model::EVENT_AFTER_POPULATE);
2034
}
2135
}

src/model/mongodb/ActiveRecord.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,26 @@
55
use yii\helpers\ArrayHelper;
66
use yii\mongodb\ActiveRecord as BaseModel;
77
use indigerd\embedded\model\Model;
8+
use indigerd\embedded\behavior\HydrateCollectionBehavior;
89

910
class ActiveRecord extends BaseModel
1011
{
1112
public function setAttributes($values, $safeOnly = true)
1213
{
1314
$this->trigger(Model::EVENT_BEFORE_POPULATE);
14-
$oldValues = $this->getAttributes(\array_keys($values));
15+
$keys = [];
16+
foreach ($values as $key => $value) {
17+
if ($this->hasAttribute($key)) {
18+
$keys[] = $key;
19+
}
20+
}
21+
$oldValues = $this->getAttributes($keys);
22+
$behaviors = $this->getBehaviors();
23+
foreach ($behaviors as $behavior) {
24+
if ($behavior instanceof HydrateCollectionBehavior and isset($values[$behavior->attribute])) {
25+
unset($oldValues[$behavior->attribute]);
26+
}
27+
}
1528
$values = ArrayHelper::merge($oldValues, $values);
1629
parent::setAttributes($values, $safeOnly);
1730
$this->trigger(Model::EVENT_AFTER_POPULATE);

0 commit comments

Comments
 (0)