diff --git a/LICENSE b/LICENSE.md similarity index 100% rename from LICENSE rename to LICENSE.md diff --git a/README.md b/README.md index 599f1c9..558deb5 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,19 @@ # Yii2 Array Field Behavior +This Yii2 model behavior allows you to store arrays in attributes. + ## Installation + Run the [Composer](http://getcomposer.org/download/) command to install the latest stable version: + ``` -composer require frostealth/yii2-array-field +composer require frostealth/yii2-array-field @stable ``` ## Usage + Just attach the behavior to your model. + ```php use frostealth\yii2\behaviors\ArrayFieldBehavior; @@ -17,9 +23,14 @@ public function behaviors() [ 'class' => ArrayFieldBehavior::className(), 'attributes' => ['attribute1', 'attribute2'], - // 'defaultEncodedValue' => 'some value', - // 'defaultDecodedValue' => 'some value', + // 'defaultEncodedValue' => null, + // 'defaultDecodedValue' => [], ], ]; } -``` \ No newline at end of file +``` + +## License + +The MIT License (MIT). +See [LICENSE.md](https://github.com/frostealth/yii2-array-field/blob/master/LICENSE.md) for more information. \ No newline at end of file diff --git a/composer.json b/composer.json index f2f5b6a..46ecf4d 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "frostealth/yii2-array-field", - "description": "Yii2 Array Field Behavior", + "description": "Behavior that allows you to store arrays in attributes", "keywords": ["yii2", "array", "attribute", "json", "behavior"], "license": "MIT", "homepage": "https://github.com/frostealth/yii2-array-field", diff --git a/src/behaviors/ArrayFieldBehavior.php b/src/behaviors/ArrayFieldBehavior.php index 0fee904..5a657ef 100644 --- a/src/behaviors/ArrayFieldBehavior.php +++ b/src/behaviors/ArrayFieldBehavior.php @@ -3,8 +3,7 @@ namespace frostealth\yii2\behaviors; use yii\base\Behavior; -use yii\db\ActiveRecord; -use yii\helpers\ArrayHelper; +use yii\db\BaseActiveRecord; use yii\helpers\Json; /** @@ -26,7 +25,7 @@ * } * ~~~ * - * @property ActiveRecord $owner + * @property BaseActiveRecord $owner * * @package frostealth\yii2\behaviors */ @@ -63,11 +62,11 @@ class ArrayFieldBehavior extends Behavior public function events() { return [ - ActiveRecord::EVENT_AFTER_FIND => 'decode', - ActiveRecord::EVENT_AFTER_INSERT => 'decode', - ActiveRecord::EVENT_AFTER_UPDATE => 'decode', - ActiveRecord::EVENT_BEFORE_UPDATE => 'encode', - ActiveRecord::EVENT_BEFORE_INSERT => 'encode', + BaseActiveRecord::EVENT_AFTER_FIND => 'decode', + BaseActiveRecord::EVENT_AFTER_INSERT => 'decode', + BaseActiveRecord::EVENT_AFTER_UPDATE => 'decode', + BaseActiveRecord::EVENT_BEFORE_UPDATE => 'encode', + BaseActiveRecord::EVENT_BEFORE_INSERT => 'encode', ]; } @@ -77,9 +76,8 @@ public function events() public function encode() { foreach ($this->attributes as $attribute) { - if (!$this->owner->getIsNewRecord()) { - $oldValue = ArrayHelper::getValue($this->_oldAttributes, $this->defaultEncodedValue); - $this->owner->setOldAttribute($attribute, $oldValue); + if (isset($this->_oldAttributes[$attribute])) { + $this->owner->setOldAttribute($attribute, $this->_oldAttributes[$attribute]); } $value = $this->owner->getAttribute($attribute); @@ -96,7 +94,7 @@ public function encode() public function decode() { foreach ($this->attributes as $attribute) { - if (!empty($this->_cache[$attribute])) { + if (isset($this->_cache[$attribute])) { $value = $this->_cache[$attribute]; } else { $value = Json::decode($this->owner->getAttribute($attribute));