jc-it/yii2-json-behavior

为Yii2添加魔法属性的JSON行为。非常适合单表继承。

v1.0.0 2019-07-10 12:20 UTC

This package is auto-updated.

Last update: 2024-09-22 18:33:58 UTC


README

此扩展提供了一个基于配置属性列表实现魔法获取器和设置器的包。非常适合单表继承。

$ composer require jc-it/yii2-json-behavior

或者添加

"jc-it/yii2-json-behavior": "^<latest version>"

到你的composer.json文件的require部分。

配置

在模型中

/**
 * @return array
 */
public function behaviors(): array
{
    return ArrayHelper::merge(
        parent::behaviors(),
        [
            JsonConfigurationBehavior::class => [
                'class' => JsonConfigurationBehavior::class,
                'jsonAttributes' => [
                    '<jsonAttribute>' => [
                        '<attribute>' => '<defaultValue>'
                        '<attribute2>' => '<defaultValue2>'
                    ],
                    '<jsonAttribute2>' => [
                        '<attribute3>' => '<defaultValue3>'
                        '<attribute4>' => '<defaultValue4>'
                    ] 
                ]
            ],
        ]
    );
}

致谢

许可证

MIT许可证(MIT)。更多信息请参阅LICENSE