petrgrishin / yii2-array-field
Yii2 数组字段行为
2.0.0
2014-06-26 07:40 UTC
Requires
- php: >=5.4.0
- petrgrishin/array-access: ~2.0
- yiisoft/yii2: *
Requires (Dev)
- league/phpunit-coverage-listener: ~1.0
- yiisoft/yii2: 2.0.0-beta
- yiisoft/yii2-composer: *
This package is not auto-updated.
Last update: 2024-09-14 16:07:24 UTC
README
Yii2 数组字段行为(使用方法 https://github.com/petrgrishin/array-access)
安装
将依赖项添加到您项目的 composer.json 文件中
{ "require": { "petrgrishin/yii2-array-field": "~2.0" } }
使用示例
将行为附加到您的模型中
模型具有用于存储数组的文本属性 data
namespace app\models; use yii\db\ActiveRecord; use \PetrGrishin\ArrayField\ArrayAccessFieldBehavior; class Model extends ActiveRecord{ public function behaviors() { return [ 'arrayField' => [ 'class' => ArrayAccessFieldBehavior::className(), 'fieldNameStorage' => 'data', ] ]; } }
使用行为
$model = Model::find(1)->one(); $model->arrayField->setValue('a.b', true); $value = $model->arrayField->getValue('a.b'); $array = $model->arrayField->getArray();
仅保存数组字段
$model = Model::find(1)->one(); $model->arrayField->setValue('a.b', true); $model->arrayField->save();