Behavior that convert array to JSON before save data in model
Install via composer
composer require emidia/yii2-jsonify
Or you may add dependency manually in composer.json:
"emidia/yii2-jsonify": "*"
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'
]
]);