vergelijkgroep/yii2-json-behavior
最新稳定版本:v1.0.1
Composer 安装命令:
composer require vergelijkgroep/yii2-json-behavior
包简介
Yii2 json encoding and decoding attribute behavior
关键字:
README 文档
README
Automatically encode/decode attribute values in JSON via Yii2 Behavior.
Install
Install via Composer:
composer require vergelijkgroep/yii2-json-behavior
or add
"vergelijkgroep/yii2-json-behavior" : "*"
to the require section of your composer.json file.
Usage
JsonBehavior
Configure your model:
use vergelijkgroep\JsonBehavior\JsonBehavior; class Item extends \yii\db\ActiveRecord { public function behaviors() { return [ [ 'class' => JsonBehavior::class, 'attributes' => ['attribute1', 'attribute2'], 'emptyValue' => 'empty_value', // optional 'asArray' => true, // optional ] ]; } }
The attributes will now automatically be encoded and decoded:
$item = Item::findOne(1); $item->attribute1['foo'] = 'bar'; $item->attribute2 = null; $item->save(); // attribute1 will be encoded and saved as json $item = Item::findOne(1); echo $item->attribute1['foo']; // 'bar' echo $item->attribute2; // 'empty_value'
emptyValue
The emptyValue is the value of the attribute when its null after decoding from JSON. Default is null.
asArray
The asArray option defines the JSON decoding format, true for associative array, false for object. Default is false.
License
This software is distributed under the MIT license.
统计信息
- 总下载量: 928
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-11