PHPackages                             mkiselev/yii2-serialize-attribute-behavior - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mkiselev/yii2-serialize-attribute-behavior

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

mkiselev/yii2-serialize-attribute-behavior
==========================================

Yii2 serialize attribute behavior

v1.0.0(9y ago)61.4k↓11.1%1[2 issues](https://github.com/MKiselev/yii2-serialize-attribute-behavior/issues)MITPHP

Since Jan 18Pushed 9y agoCompare

[ Source](https://github.com/MKiselev/yii2-serialize-attribute-behavior)[ Packagist](https://packagist.org/packages/mkiselev/yii2-serialize-attribute-behavior)[ RSS](/packages/mkiselev-yii2-serialize-attribute-behavior/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Yii2 Serialize Attribute Behavior
=================================

[](#yii2-serialize-attribute-behavior)

This Yii 2.0 ActiveRecord behavior allows you to store serialized values in attributes.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist mkiselev/yii2-serialize-attribute-behavior "*"

```

or add

```
"mkiselev/yii2-serialize-attribute-behavior": "*"

```

to the require section of your `composer.json` file.

Usage
-----

[](#usage)

Behavior adds property named "$attribute . $unserializedAttributeSuffix" to your ActiveRecord class.

You can get unserialized property value like `$model->propertyArray`.

You can set unserialized property value like `$model->propertyArray = []`.

### Basic

[](#basic)

If you want to just serialize and unserialize attribute use a config like the following:

```
public function behaviors()
{
    return [
        ...
        [
            'class' => SerializeAttributeBehavior::className(),
            'attribute' => 'data',
            'unserializedAttributeSuffix' => 'Array',
        ],
        ...
    ];
}
```

### Advanced

[](#advanced)

If you need more flexible logic, you may configure behavior like this:

```
public function behaviors()
{
    return [
        ...
        [
            'class' => SerializeAttributeBehavior::className(),
            'attribute' => 'data',
            'unserializedAttributeSuffix' => 'Model',
            // MyModel must extend mkiselev\serialized\Model
            'setAttributesToModel' => MyModel::className(),
            'setAttributesToModelSafeOnly' => true,
            // serializerClass must implements mkiselev\serialized\interfaces\SerializerInterface
            'serializerClass' => MySerializer::className(),
        ],
        ...
    ];
}
```

In the example above `MyModel` must contain attributes, rules, and more in order to support `ActiveField` attributes unserialization.

For example:

```
