Skip to content

Commit

Permalink
just a small patch
Browse files Browse the repository at this point in the history
  • Loading branch information
frostealth committed Nov 11, 2015
1 parent f3d404c commit 2e1719b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
File renamed without changes.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,9 +23,14 @@ public function behaviors()
[
'class' => ArrayFieldBehavior::className(),
'attributes' => ['attribute1', 'attribute2'],
// 'defaultEncodedValue' => 'some value',
// 'defaultDecodedValue' => 'some value',
// 'defaultEncodedValue' => null,
// 'defaultDecodedValue' => [],
],
];
}
```
```

## License

The MIT License (MIT).
See [LICENSE.md](https://github.com/frostealth/yii2-array-field/blob/master/LICENSE.md) for more information.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
22 changes: 10 additions & 12 deletions src/behaviors/ArrayFieldBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -26,7 +25,7 @@
* }
* ~~~
*
* @property ActiveRecord $owner
* @property BaseActiveRecord $owner
*
* @package frostealth\yii2\behaviors
*/
Expand Down Expand Up @@ -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',
];
}

Expand All @@ -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);
Expand All @@ -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));
Expand Down

0 comments on commit 2e1719b

Please sign in to comment.