Skip to content

Baloo-Mova/yii2-eav

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EAV Dynamic Attributes for Yii2

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

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

Install

Add github repository

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

and then

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

Configure

php yii migrate/up --migrationPath=@mirocow/eav/migrations

or

php yii migrate/up --migrationPath=vendor/mirocow/yii2-eav/src/migrations

Use

Model

class Product extends \yii\db\ActiveRecord
{

    use EavTrait;
    
    /**
     * create_time, update_time to now()
     * crate_user_id, update_user_id to current login user id
     */
    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' => isset()? $this->category->id: 0,
            'entityModel' => $this::className()
        ]);  
    }
    
}

View

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

Form

fo load selected field

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

or for load all fields

    <?php
    foreach($model->getEavAttributes()->all() as $attr){
        echo $form->field($model, $attr->name, ['class' => '\mirocow\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];
  }
?> 

Administrate GUI

Form

Add / Edit attribute

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

About

Dynamic Attributes for Yii2 (EAV)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 83.2%
  • PHP 13.1%
  • CSS 3.7%