sam-it/yii2-jsonbehavior

高级 AR JSON 支持


README

Scrutinizer Code Quality Code Coverage Build Status Total Downloads Latest Stable Version

Yii2 JsonBehavior

在 Yii2 中处理 JSON 字段

此行为为在 Yii AR 模型中处理 JSON 数据提供了高级支持。

像普通字段一样使用 JSON 字段

考虑一个具有 data 属性的模型,该属性以 JSON 格式存储。

public function behaviors() {
    return [
        ['class' => JsonBehavior::class, 'jsonAttributes' => ['data']]
    ];
}

// Examples:
$model = new Model();
$model->a = "test"; // If attribute 'a' does not exist this is stored inside the data.

$model->a['b'] = 'c']; // Nested arrays are supported.

$model->data = ['x' => 'y']; // Assigning directly is supported.