Skip to content

MaksimKiselev/yii2-serialize-attribute-behavior

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Yii2 Serialize Attribute Behavior

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

Installation

The preferred way to install this extension is through composer.

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

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

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

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:

<?= $form->field($model, 'activeRecordModelAttribute')->textInput(); ?>
<?= $form->field($model->dataModel, 'MyModelAttribute')->textInput(); ?>

Use cases

  • This behavior was developed primarily for use with JSON in PostgreSQL and MariaDB columns.
  • You may use it to store any serialized data as text.

About

Yii2 serialize attribute behavior

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages