2017 © Pedro Peláez
 

yii2-extension yii2-eav

EAV for Yii2

image

mirocow/yii2-eav

EAV for Yii2

  • Wednesday, April 11, 2018
  • by Mirocow
  • Repository
  • 18 Watchers
  • 68 Stars
  • 1,742 Installations
  • JavaScript
  • 0 Dependents
  • 0 Suggesters
  • 45 Forks
  • 17 Open issues
  • 9 Versions
  • 20 % Grown

The README.md

EAV Dynamic Attributes for Yii2

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

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

Screenshots

Edit attributes

List of attributes

, (*3)

Edit attribute

, (*4)

Edit form

, (*5)

Install

Add github repository

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

and then, (*6)

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

Configure

``` sh php ./yii migrate/up -p=@mirocow/eav/migrations, (*7)


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

and then add messages settings, (*8)

``` php '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', ], ], ], (*9)


## Use ### Model #### Simple ``` php 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() ]); } }

Advanced

``` php class Product extends \yii\db\ActiveRecord {, (*10)

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

}, (*11)


### View Insert this code for create widget or load all EAV inputs fields for model ### Form edit fo load selected field ``` php <?=$form->field($model,'test5', ['class' => '\mirocow\eav\widgets\ActiveField'])->eavInput(); ?>

or for load all fields, (*12)

Simple

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


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

Advanced

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


### Partial template ``` php

Encode getEavAttributes()->all() as $attr) { print_r($model[$attr->name]['value']); } ?> , (*15)

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

Add attribute

$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();

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:, (*16)

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

Form

Add / Edit attribute

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

The Versions

11/04 2018

dev-master

9999999-dev

EAV for Yii2

  Sources   Download

MIT

The Requires

 

by Alien-art

yii2 fields attribute eav cck

07/01 2017

v0.7.2

0.7.2.0

EAV for Yii2

  Sources   Download

MIT

The Requires

 

by Alien-art

yii2 fields attribute eav cck

07/01 2017

dev-pulls/71

dev-pulls/71

EAV for Yii2

  Sources   Download

MIT

The Requires

 

by Alien-art

yii2 fields attribute eav cck

25/12 2016

v0.7.1

0.7.1.0

EAV for Yii2

  Sources   Download

MIT

The Requires

 

by Alien-art

yii2 fields attribute eav cck

01/08 2016

v0.7.0

0.7.0.0

EAV for Yii2

  Sources   Download

MIT

The Requires

 

by Alien-art

yii2 fields attribute eav cck

01/08 2016

v0.6.2

0.6.2.0

EAV for Yii2

  Sources   Download

MIT

The Requires

 

by Alien-art

yii2 fields attribute eav cck

09/12 2015

v0.6.1

0.6.1.0

EAV for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 fields attribute eav cck

24/10 2015

v0.6.0

0.6.0.0

EAV for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 fields attribute eav cck

23/09 2015