shurik2k5/yii2-eav

EAV for Yii2

Installs: 3

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 59

Language:JavaScript

Type:yii2-extension

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

This package is auto-updated.

Last update: 2024-10-18 09:44:16 UTC


README

Архитектура баз данных EAV(Enity-Attribute-Value, Сущность-Атрибут-Значение)

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

Screenshots

Edit attributes

List of attributes

Edit attribute

Edit form

Install

The preferred way to install this extension is through composer.

Either run

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

Configure

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

or

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

and then add messages settings

    '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' => '@shurik2k5/eav/messages',
            ],
        ],
    ]

Use

Model

Simple

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' => \shurik2k5\eav\EavBehavior::className(),
                // это модель для таблицы object_attribute_value
                'valueClass' => \shurik2k5\eav\models\EavAttributeValue::className(),
            ]
        ];
    }
    
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEavAttributes()
    {
        return \shurik2k5\eav\models\EavAttribute::find()
            ->joinWith('entity')
            ->where([
                'categoryId' => $this->categories[0]->id,
                'entityModel' => $this::className()
        ]);
    }
}

Advanced

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' => \shurik2k5\eav\EavBehavior::className(),
                // это модель для таблицы object_attribute_value
                'valueClass' => \shurik2k5\eav\models\EavAttributeValue::className(),
            ]
        ];
    }
    
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEavAttributes($attributes = [])
    {
        return \shuri2kk5\eav\models\EavAttribute::find()
            ->joinWith('entity')
            ->where([
                //'categoryId' => $this->categories[0]->id,
                'entityModel' => $this::className()
            ])
        ->orderBy(['order' => SORT_ASC]);
    }
}

View

Insert this code for create widget or load all EAV inputs fields for model

Form edit

fo load selected field

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

or for load all fields

Simple

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

or add sorted

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

Advanced

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

Partial template

<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];
    }
?>

Add attribute

$attr = new shurik2k5\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();

Add/Update values

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

Administrate GUI

Config module EAV for managment of fields

In main config file:

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

Form

Add / Edit attribute

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