paulzi / yii2-json-behavior
Yii2 json属性行为
v1.0.7
2020-03-27 08:40 UTC
Requires
- php: >=5.4.0
- yiisoft/yii2: ~2.0.0
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-08-30 01:49:33 UTC
README
自动解码/编码属性值在json中,提供数组访问和json验证器。
警告!从版本2.0.14开始,Yii内置了DB JSON类型,因此此行为不再需要。
安装
通过Composer安装
composer require paulzi/yii2-json-behavior
或添加
"paulzi/yii2-json-behavior" : "~1.0.0"
到您的composer.json
文件的require
部分。
使用方法
JsonBehavior
配置您的模型
use paulzi\jsonBehavior\JsonBehavior; class Item extends \yii\db\ActiveRecord { public function behaviors() { return [ [ 'class' => JsonBehavior::className(), 'attributes' => ['params'], ], ]; } }
现在您可以像访问数组一样访问属性
$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
通过json字符串设置属性
$item = new Item(); $item->params->set('[2, 4, 42]'); echo $item->params[2]; // 42
通过数组设置属性
$item = new Item(); $item->params->set(['test' => ['one' => 1]]); echo $item->params['test']['one']; // 1
转换为json字符串
$item = new Item(); $item->params['test'] = ['one' => false, 'two' => [1, 2, 3]]; var_dump((string)$item->params); // {"one":false,"two":[1,2,3]}
转换为数组
$item = new Item(); $item->params->set('{ "one": 1, "two": null, "three": false, "four": "four" }'); var_dump($item->params->toArray());
检查是否为空
$item = new Item(); $item->params->set('{}'); var_dump($item->params->isEmpty()); // true
emptyValue
您可以将emptyValue
选项设置为定义空JSON值(默认null
)。可以是'{}'
、'[]''
或null
。
JsonValidator
配置您的模型(参见上面的行为配置)
use paulzi\jsonBehavior\JsonValidator; class Item extends \yii\db\ActiveRecord { public function rules() { return [ [['params'], JsonValidator::className()], ]; } }
验证
$item = new Item(); $item->attributes = ['params' => '{ test: }']; var_dump($item->save()); // false var_dump($item->errors); // ['params' => ['Value is not valid JSON or scalar']]
您可以设置merge = true
,在这种情况下,而不是替换所有传输数据的字段数据,将应用array_merge()
与字段中的旧数据(从ActiveRecord的oldAttributes
中获取)合并。此选项只能与ActiveRecord一起使用。
use paulzi\jsonBehavior\JsonValidator; class Item extends \yii\db\ActiveRecord { public function rules() { return [ [['params'], JsonValidator::className(), 'merge' => true], ]; } }
JsonField
您可以使用JsonField
类用于其他模型
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}
如何使用
使用isAttributeChanged()和getDirtyAttributes()
Yii2不提供注入代码以检查属性脏的能力。
如果您需要使用方法isAttributeChanged()
或getDirtyAttributes()
,您可以在模型中重写它们。
/** * @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; }