reshik84/yii2-eav-behavior

ActiveRecord 的 EAV 行为

安装: 22

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 1

开放问题: 0

类型:yii2-extension

dev-master 2017-08-22 10:23 UTC

This package is not auto-updated.

Last update: 2024-09-29 03:16:13 UTC


README

EAV-behavior

============

ActiveRecord 的 EAV 行为

安装

安装此扩展的首选方式是通过 composer.

运行以下命令之一


php composer.phar require --prefer-dist reshik84/yii2-eav-behavior "*"


"reshik84/yii2-eav-behavior": "*"

将以下内容添加到您的 composer.json 文件的 require 部分。


$ php yii migrate/up --migrationPath=@vendor/reshik84/yii2-eav-behavior/migrations

使用方法

扩展安装后,将其添加到您的模型中

    public function behaviors()
    {
        return [
            'eav' => [
                'class' => resh\eav\behaviors\EavBehavior::className(),
                'model_id' => 'id' // primary key
            ]
        ];
    }
    
    // list of EAV attributes
    public function additionalAttributes(){
        return ['attr1', 'attr2', 'attr3', ... ];
    }

    public function rules() {
        return [
            [['attr1', 'attr2', 'attr3'], 'safe']
        ];
    }
    
    public function attributeLabels()
    {
        return [
            'attr1' => 'Attribute 1',
            'attr2' => 'Attribute 2',
            'attr3' => 'Attribute 3',
        ];
    }

调用 EAV 属性

$model = new Model();
$attr = $model->attr1; // get value
$model->attr1 = 'some value'; // set value
$model->save();