2017 © Pedro Peláez
 

yii2-extension yii2-serialize-attribute-behavior

Yii2 serialize attribute behavior

image

mkiselev/yii2-serialize-attribute-behavior

Yii2 serialize attribute behavior

  • Thursday, January 19, 2017
  • by MKiselev
  • Repository
  • 0 Watchers
  • 5 Stars
  • 92 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 2 Open issues
  • 2 Versions
  • 21 % Grown

The README.md

Yii2 Serialize Attribute Behavior

This Yii 2.0 ActiveRecord behavior allows you to store serialized values in attributes., (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

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

or add, (*4)

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

to the require section of your composer.json file., (*5)

Usage

Behavior adds property named "$attribute . $unserializedAttributeSuffix" to your ActiveRecord class., (*6)

You can get unserialized property value like $model->propertyArray., (*7)

You can set unserialized property value like $model->propertyArray = []., (*8)

Basic

If you want to just serialize and unserialize attribute use a config like the following:, (*9)

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

Advanced

If you need more flexible logic, you may configure behavior like this:, (*10)

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., (*11)

For example:, (*12)

<?= $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.

The Versions

19/01 2017

dev-master

9999999-dev

Yii2 serialize attribute behavior

  Sources   Download

MIT

The Requires

 

by Maksim Kiselev

json yii2 array yii behavior serialize attributes attribute serialized

18/01 2017

v1.0.0

1.0.0.0

Yii2 serialize attribute behavior

  Sources   Download

MIT

The Requires

 

by Maksim Kiselev

json yii2 array yii behavior serialize attributes attribute serialized