Skip to content

Behavior that convert array to JSON before save data in model, and also convert JSON to an array after saving and retrieving data

License

Notifications You must be signed in to change notification settings

laxity7/yii2-json-field

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Yii2 JsonFieldBehavior

License Latest Stable Version Total Downloads

This behavior adds advanced support for working with JSON data in Yii2 active record models. Behavior convert array to JSON before save data in model, and also convert JSON to an array after saving and retrieving data. Use JSON fields like normal fields with an array or object.

Install

Install via composer

composer require laxity7/yii2-json-field

How to use

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

/** @inheritdoc */
public function behaviors(): array
{
   return [
       [
           'class'  => \laxity7\yii2\behaviors\JsonFieldBehavior::class,
           'fields' => ['foo_data', 'bar_data'],
       ],
   ];
}

You can also pass the following parameters:

  • jsonOptions int (by default JSON_UNESCAPED_UNICODE) JSON constants can be combined to form options for json_encode().
  • defaultValue array|string (by default '[]') The default value for attribute. This value by default will be stored in the database if the field value is empty. Ignored if [[skipEmpty]] is enabled.
  • skipEmpty bool (by default true) Whether to skip a field if it's empty. When TRUE in the database, the field can be null, when FALSE will save an empty object ('[]' or see defaultValue)
  • asArray bool (by default true) Decode JSON into an array or object.

So, the complete list of settings will look like this:

use laxity7\yii2\behaviors\JsonFieldBehavior;
use yii\db\ActiveRecord;

/**
* @property int $id
* @property array $foo_data
* @property array $bar_data
 */
class Foo extends ActiveRecord {
    /** @inheritdoc */
    public function behaviors(): array
    {
        return [
            [
                'class'        => JsonFieldBehavior::class,
                'fields'       => ['foo_data', 'bar_data'],
                'jsonOptions'  => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
                'skipEmpty'    => false,
                'defaultValue' => ['foo' => 'bar'],
                'asArray'      => true,
            ],
        ];
    }
    
    // Based on these parameters may be approximately the code
    public function updateBar(int $id, array $barData): array
    {
        $model = self::findOne(['id' => $id]);
        $model->foo_data['foo'] = 'bar1';
        $model->bar_data['bar'] = array_merge($model->bar_data['bar'], $barData);
        $model->save();
    
        return $model->bar_data['bar'];
    }
}

About

Behavior that convert array to JSON before save data in model, and also convert JSON to an array after saving and retrieving data

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages