Skip to content

Commit 2e1719b

Browse files
committed
just a small patch
1 parent f3d404c commit 2e1719b

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

LICENSE renamed to LICENSE.md

File renamed without changes.

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
# Yii2 Array Field Behavior
22

3+
This Yii2 model behavior allows you to store arrays in attributes.
4+
35
## Installation
6+
47
Run the [Composer](http://getcomposer.org/download/) command to install the latest stable version:
8+
59
```
6-
composer require frostealth/yii2-array-field
10+
composer require frostealth/yii2-array-field @stable
711
```
812

913
## Usage
14+
1015
Just attach the behavior to your model.
16+
1117
```php
1218
use frostealth\yii2\behaviors\ArrayFieldBehavior;
1319

@@ -17,9 +23,14 @@ public function behaviors()
1723
[
1824
'class' => ArrayFieldBehavior::className(),
1925
'attributes' => ['attribute1', 'attribute2'],
20-
// 'defaultEncodedValue' => 'some value',
21-
// 'defaultDecodedValue' => 'some value',
26+
// 'defaultEncodedValue' => null,
27+
// 'defaultDecodedValue' => [],
2228
],
2329
];
2430
}
25-
```
31+
```
32+
33+
## License
34+
35+
The MIT License (MIT).
36+
See [LICENSE.md](https://github.com/frostealth/yii2-array-field/blob/master/LICENSE.md) for more information.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "frostealth/yii2-array-field",
3-
"description": "Yii2 Array Field Behavior",
3+
"description": "Behavior that allows you to store arrays in attributes",
44
"keywords": ["yii2", "array", "attribute", "json", "behavior"],
55
"license": "MIT",
66
"homepage": "https://github.com/frostealth/yii2-array-field",

src/behaviors/ArrayFieldBehavior.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
namespace frostealth\yii2\behaviors;
44

55
use yii\base\Behavior;
6-
use yii\db\ActiveRecord;
7-
use yii\helpers\ArrayHelper;
6+
use yii\db\BaseActiveRecord;
87
use yii\helpers\Json;
98

109
/**
@@ -26,7 +25,7 @@
2625
* }
2726
* ~~~
2827
*
29-
* @property ActiveRecord $owner
28+
* @property BaseActiveRecord $owner
3029
*
3130
* @package frostealth\yii2\behaviors
3231
*/
@@ -63,11 +62,11 @@ class ArrayFieldBehavior extends Behavior
6362
public function events()
6463
{
6564
return [
66-
ActiveRecord::EVENT_AFTER_FIND => 'decode',
67-
ActiveRecord::EVENT_AFTER_INSERT => 'decode',
68-
ActiveRecord::EVENT_AFTER_UPDATE => 'decode',
69-
ActiveRecord::EVENT_BEFORE_UPDATE => 'encode',
70-
ActiveRecord::EVENT_BEFORE_INSERT => 'encode',
65+
BaseActiveRecord::EVENT_AFTER_FIND => 'decode',
66+
BaseActiveRecord::EVENT_AFTER_INSERT => 'decode',
67+
BaseActiveRecord::EVENT_AFTER_UPDATE => 'decode',
68+
BaseActiveRecord::EVENT_BEFORE_UPDATE => 'encode',
69+
BaseActiveRecord::EVENT_BEFORE_INSERT => 'encode',
7170
];
7271
}
7372

@@ -77,9 +76,8 @@ public function events()
7776
public function encode()
7877
{
7978
foreach ($this->attributes as $attribute) {
80-
if (!$this->owner->getIsNewRecord()) {
81-
$oldValue = ArrayHelper::getValue($this->_oldAttributes, $this->defaultEncodedValue);
82-
$this->owner->setOldAttribute($attribute, $oldValue);
79+
if (isset($this->_oldAttributes[$attribute])) {
80+
$this->owner->setOldAttribute($attribute, $this->_oldAttributes[$attribute]);
8381
}
8482

8583
$value = $this->owner->getAttribute($attribute);
@@ -96,7 +94,7 @@ public function encode()
9694
public function decode()
9795
{
9896
foreach ($this->attributes as $attribute) {
99-
if (!empty($this->_cache[$attribute])) {
97+
if (isset($this->_cache[$attribute])) {
10098
$value = $this->_cache[$attribute];
10199
} else {
102100
$value = Json::decode($this->owner->getAttribute($attribute));

0 commit comments

Comments
 (0)