File tree Expand file tree Collapse file tree 3 files changed +45
-5
lines changed Expand file tree Collapse file tree 3 files changed +45
-5
lines changed Original file line number Diff line number Diff line change 44
55use yii \helpers \ArrayHelper ;
66use yii \db \ActiveRecord as BaseModel ;
7+ use indigerd \embedded \behavior \HydrateCollectionBehavior ;
78
89class 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 );
Original file line number Diff line number Diff line change 44
55use yii \helpers \ArrayHelper ;
66use yii \base \Model as BaseModel ;
7+ use indigerd \embedded \behavior \HydrateCollectionBehavior ;
78
89class 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}
Original file line number Diff line number Diff line change 55use yii \helpers \ArrayHelper ;
66use yii \mongodb \ActiveRecord as BaseModel ;
77use indigerd \embedded \model \Model ;
8+ use indigerd \embedded \behavior \HydrateCollectionBehavior ;
89
910class 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 );
You can’t perform that action at this time.
0 commit comments