laxity7/yii2-json-field 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laxity7/yii2-json-field

最新稳定版本:v1.0.2

Composer 安装命令:

composer require laxity7/yii2-json-field

包简介

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

README 文档

README

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

统计信息

  • 总下载量: 80.16k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-02-20