blacksesion/yii2-eav

EAV para Yii2

Installs: 1 631

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 2

Forks: 59

Language:JavaScript

Type:yii2-extension

1.1.3 2018-04-06 17:35 UTC

This package is not auto-updated.

Last update: 2024-04-17 08:03:16 UTC


README

EAV(Enity-Attribute-Value) Tomado del trabajo de mirocow

Screenshots

Edit attributes

List of attributes

Edit attribute

Edit form

Active field rules

Install

Add github repository

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

and then

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

Configure

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

or

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

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
        ];
    }
    
    /**
     * create_time, update_time to now()
     * crate_user_id, update_user_id to current login user id
     */
    public function behaviors()
    {
        return [
            'eav' => [
                'class' => \blacksesion\eav\EavBehavior::className(),
                'valueClass' => \blacksesion\eav\models\EavAttributeValue::className(),
            ]           
        ];
    }    

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEavAttributes()
    {
        return \blacksesion\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
        ];
    }

    /**
     * create_time, update_time to now()
     * crate_user_id, update_user_id to current login user id
     */
    public function behaviors()
    {
        return [
            'eav' => [
                'class' => \blacksesion\eav\EavBehavior::className(),
                'valueClass' => \blacksesion\eav\models\EavAttributeValue::className(),
            ]
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEavAttributes($attributes = [])
    {
        return \blacksesion\eav\models\EavAttribute::find()
          ->joinWith('entity')
          ->where([
            //'categoryId' => $entityId,
            'entityModel' => $this::className()
        ])->andWhere($attributes);
    }

}

### 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' => '\blacksesion\eav\widgets\ActiveField'])->eavInput(); ?>

or for load all fields

Simple

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

Advanced

    <?php
    foreach($model->getEavAttributes(['entityId' => 8, 'typeId' => 3])->all() as $attr){
        echo $form->field($model, $attr->name, ['class' => '\blacksesion\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 blacksesion\eav\models\EavAttribute();
$attr->attributes = [
        'entityId' => 1, // Category ID
        'name' => 'AttrCategory1',  // service name field
        'label' => 'Attr1',         // label text for form
        'defaultValue' => 'attr1',  // default value
        'entityModel' => SampleModel::className(), // work model
        'required'=>false           // add rule "required field"
    ];
$attr->save();

Administrate GUI

Config module EAV for managment of fields

In main config file:

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

Form

Add / Edit attribute

<?= \blacksesion\eav\admin\widgets\Fields::widget([
                      'model' => $model,
                      'categoryId' => $model->id,
                      'entityName' => 'Entidad',
                      'entityModel' => 'common\models\Product',
                  ])?>