承接 paulzi/yii2-json-behavior 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

paulzi/yii2-json-behavior

Composer 安装命令:

composer require paulzi/yii2-json-behavior

包简介

Yii2 json attribute behavior

README 文档

README

Auto decode/encode attribute value in json, provide array access and json validator.

WARNING! From version 2.0.14 Yii has built-in DB JSON-type, and this behavior is no longer required.

Russian readme

Packagist Version Code Coverage Scrutinizer Build Status Total Downloads

Install

Install via Composer:

composer require paulzi/yii2-json-behavior

or add

"paulzi/yii2-json-behavior" : "~1.0.0"

to the require section of your composer.json file.

Usage

JsonBehavior

Configure your model:

use paulzi\jsonBehavior\JsonBehavior;

class Item extends \yii\db\ActiveRecord
{
    public function behaviors() {
        return [
            [
                'class' => JsonBehavior::className(),
                'attributes' => ['params'],
            ],
        ];
    }
}

Now you can access to attribute as array:

$item = Item::findOne(1);
$item->params['one'] = 'two';
$item->params['two'] = [];
$item->params['two']['key'] = true;
$item->save();

$item = Item::findOne(1);
echo $item['two']['key']; // true

Set attribute via json string:

$item = new Item();
$item->params->set('[2, 4, 42]');
echo $item->params[2]; // 42

Set attribute via array:

$item = new Item();
$item->params->set(['test' => ['one' => 1]]);
echo $item->params['test']['one']; // 1

Convert to json string:

$item = new Item();
$item->params['test'] = ['one' => false, 'two' => [1, 2, 3]];
var_dump((string)$item->params); // {"one":false,"two":[1,2,3]}

Convert to array:

$item = new Item();
$item->params->set('{ "one": 1, "two": null, "three": false, "four": "four" }');
var_dump($item->params->toArray());

Check empty:

$item = new Item();
$item->params->set('{}');
var_dump($item->params->isEmpty()); // true

emptyValue

You can set emptyValue option to define an empty JSON value (default null). Can be '{}', '[]'' or null.

JsonValidator

Configure your model (see behavior config upper):

use paulzi\jsonBehavior\JsonValidator;

class Item extends \yii\db\ActiveRecord
{
    public function rules() {
        return [
            [['params'], JsonValidator::className()],
        ];
    }
}

Validate:

$item = new Item();
$item->attributes = ['params' => '{ test: }'];
var_dump($item->save()); // false
var_dump($item->errors); // ['params' => ['Value is not valid JSON or scalar']]

You can set merge = true, in this case, instead of replacing all field data of the transmitted data, array_merge() will be applied with old data in the field (which are taken from oldAttributes of ActiveRecord). This option can be apply only with ActiveRecord:

use paulzi\jsonBehavior\JsonValidator;

class Item extends \yii\db\ActiveRecord
{
    public function rules() {
        return [
            [['params'], JsonValidator::className(), 'merge' => true],
        ];
    }
}

JsonField

You can use JsonField class for other models:

class Item
{
    public $params;
    
    public function __constructor()
    {
        $this->params = new JsonField();
    }
}

// ...

$item = new Item();
$item->params['one'] = 1;
var_dump((string)$item->params); // {"one":1}

How To

Usage isAttributeChanged() and getDirtyAttributes()

Yii2 does not provide the ability to inject code to check attribute dirty.

If you need to use methods isAttributeChanged() or getDirtyAttributes(), you can override them in model:

/**
 * @inheritdoc
 */
public function isAttributeChanged($name, $identical = true)
{
    if ($this->$name instanceof JsonField) {
        return (string)$this->$name !== $this->getOldAttribute($name);
    } else {
        return parent::isAttributeChanged($name, $identical);
    }
}

/**
 * @inheritdoc
 */
public function getDirtyAttributes($names = null)
{
    $result = [];
    $data = parent::getDirtyAttributes($names);
    foreach ($data as $name => $value) {
        if ($value instanceof JsonField) {
            if ((string)$value !== $this->getOldAttribute($name)) {
                $result[$name] = $value;
            }
        } else {
            $result[$name] = $value;
        }
    }
    return $result;
}

paulzi/yii2-json-behavior 适用场景与选型建议

paulzi/yii2-json-behavior 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 546.08k 次下载、GitHub Stars 达 75, 最近一次更新时间为 2016 年 08 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「json」 「validator」 「Behavior」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 paulzi/yii2-json-behavior 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 paulzi/yii2-json-behavior 我们能提供哪些服务?
定制开发 / 二次开发

基于 paulzi/yii2-json-behavior 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 546.08k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 76
  • 点击次数: 22
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 75
  • Watchers: 7
  • Forks: 16
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-08-17