Skip to content

Behavior that convert array to JSON before save data in model

License

Notifications You must be signed in to change notification settings

emidia/yii2-jsonify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Yii2 JSONify

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'
  ]
]);

About

Behavior that convert array to JSON before save data in model

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages