emidia/yii2-jsonify

Behavior that convert array to JSON before save data in model

v1.0 2015-09-25 16:19 UTC

This package is not auto-updated.

Last update: 2025-06-07 21:57:24 UTC


README

Behavior that convert array to JSON before save data in model

Install

Install via composer

composer require emidia/yii2-jsonify

Or you may add dependency manually in composer.json:

 "emidia/yii2-jsonify": "*"

How to use

To use JsonifyBehavior, insert the following code to your ActiveRecord class:

use emidia\yii2\JsonifyBehavior;
public function behaviors()
{
    return [
        JsonifyBehavior::className(),
    ];
}

By default JsonifyBehavior fill json_data attribute from array into a json encoded string

If your attribute names are different, you may configure the [[attribute]] properties like the following:

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

So, if set an array in model's attribute, this behavior will convert all data to JSON

$model->setAttributes([
  'data' => [
    'id'=> 12,
    'title' => 'test'
  ]
]);