Skip to content

Commit f3d404c

Browse files
committed
improve
1 parent 5cf25cd commit f3d404c

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "frostealth/yii2-array-field",
33
"description": "Yii2 Array Field Behavior",
4+
"keywords": ["yii2", "array", "attribute", "json", "behavior"],
45
"license": "MIT",
56
"homepage": "https://github.com/frostealth/yii2-array-field",
67
"type": "yii2-extension",

src/behaviors/ArrayFieldBehavior.php

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

55
use yii\base\Behavior;
66
use yii\db\ActiveRecord;
7+
use yii\helpers\ArrayHelper;
78
use yii\helpers\Json;
89

910
/**
@@ -25,6 +26,8 @@
2526
* }
2627
* ~~~
2728
*
29+
* @property ActiveRecord $owner
30+
*
2831
* @package frostealth\yii2\behaviors
2932
*/
3033
class ArrayFieldBehavior extends Behavior
@@ -44,13 +47,25 @@ class ArrayFieldBehavior extends Behavior
4447
*/
4548
public $defaultDecodedValue = [];
4649

50+
/**
51+
* @var array
52+
*/
53+
private $_cache = [];
54+
55+
/**
56+
* @var array
57+
*/
58+
private $_oldAttributes = [];
59+
4760
/**
4861
* @inheritdoc
4962
*/
5063
public function events()
5164
{
5265
return [
5366
ActiveRecord::EVENT_AFTER_FIND => 'decode',
67+
ActiveRecord::EVENT_AFTER_INSERT => 'decode',
68+
ActiveRecord::EVENT_AFTER_UPDATE => 'decode',
5469
ActiveRecord::EVENT_BEFORE_UPDATE => 'encode',
5570
ActiveRecord::EVENT_BEFORE_INSERT => 'encode',
5671
];
@@ -62,10 +77,16 @@ public function events()
6277
public function encode()
6378
{
6479
foreach ($this->attributes as $attribute) {
65-
$value = $this->owner->{$attribute};
66-
$value = !empty($value) ? Json::encode($value) : $this->defaultEncodedValue;
80+
if (!$this->owner->getIsNewRecord()) {
81+
$oldValue = ArrayHelper::getValue($this->_oldAttributes, $this->defaultEncodedValue);
82+
$this->owner->setOldAttribute($attribute, $oldValue);
83+
}
84+
85+
$value = $this->owner->getAttribute($attribute);
86+
$this->_cache[$attribute] = $value;
6787

68-
$this->owner->{$attribute} = $value;
88+
$value = !empty($value) ? Json::encode($value) : $this->defaultEncodedValue;
89+
$this->owner->setAttribute($attribute, $value);
6990
}
7091
}
7192

@@ -75,9 +96,21 @@ public function encode()
7596
public function decode()
7697
{
7798
foreach ($this->attributes as $attribute) {
78-
$value = Json::decode($this->owner->{$attribute});
99+
if (!empty($this->_cache[$attribute])) {
100+
$value = $this->_cache[$attribute];
101+
} else {
102+
$value = Json::decode($this->owner->getAttribute($attribute));
103+
}
104+
105+
$value = !empty($value) ? $value : $this->defaultDecodedValue;
106+
$this->owner->setAttribute($attribute, $value);
79107

80-
$this->owner->{$attribute} = !empty($value) ? $value : $this->defaultDecodedValue;
108+
if (!$this->owner->getIsNewRecord()) {
109+
$this->_oldAttributes[$attribute] = $this->owner->getOldAttribute($attribute);
110+
$this->owner->setOldAttribute($attribute, $value);
111+
}
81112
}
113+
114+
$this->_cache = [];
82115
}
83116
}

0 commit comments

Comments
 (0)