mirocow/yii2-eav

Yii2 的 EAV

安装数: 6,514

依赖者: 0

建议者: 0

安全: 0

星标: 90

关注者: 17

分支: 59

公开问题: 22

语言:JavaScript

类型:yii2-extension

v0.7.2 2017-01-07 19:07 UTC

This package is auto-updated.

Last update: 2024-08-29 04:03:26 UTC


README

EAV 数据库架构(Entity-Attribute-Value,实体-属性-值)

Latest Stable Version Latest Unstable Version Total Downloads License Join the chat at https://gitter.im/Mirocow/yii2-eav

屏幕截图

编辑属性

属性列表

编辑属性

编辑表单

安装

添加 GitHub 仓库

"repositories": [
  {
    "type": "git",
    "url": "https://github.com/mirocow/yii2-eav.git"
  }
]

然后

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

配置

php ./yii migrate/up -p=@mirocow/eav/migrations

或者

php ./yii migrate/up -p=@vendor/mirocow/yii2-eav/src/migrations

然后添加消息设置

    'i18n' => [
        'translations' => [
            'app*' => [
                'class' => 'yii\i18n\PhpMessageSource',
                //'basePath' => '@app/messages',
                //'sourceLanguage' => 'en-US',
                'fileMap' => [
                    'app'       => 'app.php',
                    'app/error' => 'error.php',
                ],
            ],
            'eav' => [
                'class' => 'yii\i18n\PhpMessageSource',
                'basePath' => '@mirocow/eav/messages',
            ],
        ],
    ]

使用

模型

简单

class Product extends \yii\db\ActiveRecord
{
    public function rules()
    {
        return [
            [['name'], 'string', 'max' => 255], // Product field
            [['c1'], 'required'], // Attribute field
            [['c1'], 'string', 'max' => 255], // Attribute field
        ];
    }
    
    public function behaviors()
    {
        return [
            'eav' => [
                'class' => \mirocow\eav\EavBehavior::className(),
                // это модель для таблицы object_attribute_value
                'valueClass' => \mirocow\eav\models\EavAttributeValue::className(),
            ]
        ];
    }
    
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEavAttributes()
    {
        return \mirocow\eav\models\EavAttribute::find()
            ->joinWith('entity')
            ->where([
                'categoryId' => $this->categories[0]->id,
                'entityModel' => $this::className()
        ]);
    }
}

高级

class Product extends \yii\db\ActiveRecord
{
  
    public function rules()
    {
        return [
            [['name'], 'string', 'max' => 255], // Product field
            [['c1'], 'required'], // Attribute field
            [['c1'], 'string', 'max' => 255], // Attribute field
        ];
    }
    
    public function behaviors()
    {
        return [
            'eav' => [
                'class' => \mirocow\eav\EavBehavior::className(),
                // это модель для таблицы object_attribute_value
                'valueClass' => \mirocow\eav\models\EavAttributeValue::className(),
            ]
        ];
    }
    
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEavAttributes($attributes = [])
    {
        return \mirocow\eav\models\EavAttribute::find()
            ->joinWith('entity')
            ->where([
                //'categoryId' => $this->categories[0]->id,
                'entityModel' => $this::className()
            ])
        ->orderBy(['order' => SORT_ASC]);
    }
}

视图

插入此代码以创建小部件或加载模型的所有 EAV 输入字段

表单编辑

为加载选定字段

<?=$form->field($model,'test5', ['class' => '\mirocow\eav\widgets\ActiveField'])->eavInput(); ?>

或为加载所有字段

简单

<?php
foreach($model->getEavAttributes()->all() as $attr) {
    echo $form->field($model, $attr->name, ['class' => '\mirocow\eav\widgets\ActiveField'])->eavInput();
}
?>

或添加排序

<?php
foreach($model->getEavAttributes()->orderBy(['order' => SORT_ASC])->all() as $attr) {
    echo $form->field($model, $attr->name, ['class' => '\mirocow\eav\widgets\ActiveField'])->eavInput();
}
?>

高级

<?php
foreach($model->getEavAttributes(['entityId' => 8, 'typeId' => 3])->all() as $attr) {
    echo $form->field($model, $attr->name, ['class' => '\mirocow\eav\widgets\ActiveField'])->eavInput();
}
?>

部分模板

<p>
Encode

<?php
    foreach($model->getEavAttributes()->all() as $attr) {
        print_r($model[$attr->name]['value']);
    }
?>
</p>

<p>
String

<?php
    foreach($model->getEavAttributes()->all() as $attr){
        echo $model[$attr->name];
    }
?>

添加属性

$attr = new mirocow\eav\models\EavAttribute();
$attr->attributes = [
    'entityId' => 1,                        // Category ID
    'typeId' => 1,                          // ID type from eav_attribute_type
    'name' => 'packing',                    // service name field
    'label' => 'Packing',                   // label text for form
    'defaultValue' => '10 kg',              // default value
    'entityModel' => Product::className(),  // work model
    'required' => false                     // add rule "required field"
];
$attr->save();

$attr->attributes = [
    'entityId' => 1,                        // Category ID
    'typeId' => 1,                          // ID type from eav_attribute_type
    'name' => 'color',                      // service name field
    'label' => 'Color',                     // label text for form
    'defaultValue' => 'white',              // default value
    'entityModel' => Product::className(),  // work model
    'required' => false                     // add rule "required field"
];
$attr->save();

添加/更新值

$model = Product::find()->where(['id' => 1])->one();
$model->color = "blue";
$model->packing = "12 kg";
$model->save();

管理 GUI

配置 EAV 模块以管理字段

在主配置文件中

$modules = [
        ...,
        'eav' => [
            'class' => 'mirocow\eav\Module',
        ],
];

表单

添加/编辑属性

<?= \mirocow\eav\admin\widgets\Fields::widget([
    'model' => $model,
    'categoryId' => $model->id,
    'entityName' => 'Продукт',
    'entityModel' => 'app\models\Product',
])?>