PHPackages                             blacksesion/yii2-eav - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. blacksesion/yii2-eav

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

blacksesion/yii2-eav
====================

EAV para Yii2

1.1.3(8y ago)11.6k1MITJavaScriptPHP &gt;=5.5.0

Since Sep 23Pushed 8y ago2 watchersCompare

[ Source](https://github.com/blacksesion/yii2-eav)[ Packagist](https://packagist.org/packages/blacksesion/yii2-eav)[ RSS](/packages/blacksesion-yii2-eav/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (4)Dependencies (1)Versions (11)Used By (0)

EAV Atributos Dinamicos para Yii2
=================================

[](#eav-atributos-dinamicos-para-yii2)

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

Screenshots
===========

[](#screenshots)

Edit attributes
---------------

[](#edit-attributes)

### List of attributes

[](#list-of-attributes)

[![](https://camo.githubusercontent.com/a03a15caa3d0b977ddfa5f6b4df029147543e369629ab3969be6d7bd5ff9ace7/687474703a2f2f696d616765732e6d69726f636f772e636f6d2f323031362d30352d30322d32332d32392d33392d33686368612e706e67)](https://camo.githubusercontent.com/a03a15caa3d0b977ddfa5f6b4df029147543e369629ab3969be6d7bd5ff9ace7/687474703a2f2f696d616765732e6d69726f636f772e636f6d2f323031362d30352d30322d32332d32392d33392d33686368612e706e67)

### Edit attribute

[](#edit-attribute)

[![](https://camo.githubusercontent.com/8a7f640a708785744b81857d6a2322986747a99e82c4d53d8a07ec8736986242/687474703a2f2f696d616765732e6d69726f636f772e636f6d2f323031362d30352d30322d32332d33392d34312d35696836752e706e67)](https://camo.githubusercontent.com/8a7f640a708785744b81857d6a2322986747a99e82c4d53d8a07ec8736986242/687474703a2f2f696d616765732e6d69726f636f772e636f6d2f323031362d30352d30322d32332d33392d34312d35696836752e706e67)

Edit form
---------

[](#edit-form)

[![](https://camo.githubusercontent.com/d75d99b972129988b17fab344bdb80a9fbc6445adcaed1f766dee5088ff496e0/687474703a2f2f696d616765732e6d69726f636f772e636f6d2f323031362d30352d30322d32332d33322d33342d6d39386f312e706e67)](https://camo.githubusercontent.com/d75d99b972129988b17fab344bdb80a9fbc6445adcaed1f766dee5088ff496e0/687474703a2f2f696d616765732e6d69726f636f772e636f6d2f323031362d30352d30322d32332d33322d33342d6d39386f312e706e67)

Active field rules
------------------

[](#active-field-rules)

[![](https://camo.githubusercontent.com/5af78e738bc161fb6d5ba7360c40ebb668812a565a42fd96d6e6df3d65cbfb4f/68747470733a2f2f332e646f776e6c6f616465722e6469736b2e79616e6465782e72752f6469736b2f313061336135363430336332653638666237303135353538613537323665373766353539393765323937613562623130613034396332333130653935386164622f35373965336632652f3755712d6e54376e384a5f5576616774596a474164564a792d3637327a46624d7475547a5f6e6c6a484f33574778714f393767527471425069324d68596c5a4e715056786d6e6a752d516c376b596241596f5f3330772533442533443f7569643d302666696c656e616d653d323031362d30372d33315f31372d30392d33392e706e6726646973706f736974696f6e3d696e6c696e6526686173683d266c696d69743d3026636f6e74656e745f747970653d696d616765253246706e67266673697a653d3434393633266869643d6661326662663431393937666666343037363561313737363137343731326538266d656469615f747970653d696d61676526746b6e763d763226657461673d6430653334343732323566306434373537313037373434663937373462613238)](https://camo.githubusercontent.com/5af78e738bc161fb6d5ba7360c40ebb668812a565a42fd96d6e6df3d65cbfb4f/68747470733a2f2f332e646f776e6c6f616465722e6469736b2e79616e6465782e72752f6469736b2f313061336135363430336332653638666237303135353538613537323665373766353539393765323937613562623130613034396332333130653935386164622f35373965336632652f3755712d6e54376e384a5f5576616774596a474164564a792d3637327a46624d7475547a5f6e6c6a484f33574778714f393767527471425069324d68596c5a4e715056786d6e6a752d516c376b596241596f5f3330772533442533443f7569643d302666696c656e616d653d323031362d30372d33315f31372d30392d33392e706e6726646973706f736974696f6e3d696e6c696e6526686173683d266c696d69743d3026636f6e74656e745f747970653d696d616765253246706e67266673697a653d3434393633266869643d6661326662663431393937666666343037363561313737363137343731326538266d656469615f747970653d696d61676526746b6e763d763226657461673d6430653334343732323566306434373537313037373434663937373462613238)

Install
=======

[](#install)

Add github repository
---------------------

[](#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
---------

[](#configure)

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

or

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

Use
---

[](#use)

### Model

[](#model)

#### Simple

[](#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

[](#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
