PHPackages                             mirocow/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. [Framework](/categories/framework)
4. /
5. mirocow/yii2-eav

ActiveYii2-extension[Framework](/categories/framework)

mirocow/yii2-eav
================

EAV for Yii2

v0.7.2(9y ago)967.5k41[21 issues](https://github.com/Mirocow/yii2-eav/issues)[1 PRs](https://github.com/Mirocow/yii2-eav/pulls)MITJavaScriptPHP &gt;=5.5.0

Since Sep 23Pushed 5y ago16 watchersCompare

[ Source](https://github.com/Mirocow/yii2-eav)[ Packagist](https://packagist.org/packages/mirocow/yii2-eav)[ RSS](/packages/mirocow-yii2-eav/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (8)Used By (0)

EAV Dynamic Attributes for Yii2
===============================

[](#eav-dynamic-attributes-for-yii2)

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

[![Latest Stable Version](https://camo.githubusercontent.com/022bca732c44c466c1b141af0375bd05b5bce7dc4391a5b6784f6c98ca371b35/68747470733a2f2f706f7365722e707567782e6f72672f6d69726f636f772f796969322d6561762f762f737461626c65)](https://packagist.org/packages/mirocow/yii2-eav) [![Latest Unstable Version](https://camo.githubusercontent.com/435ac5bef9111b8d4eed8193d884e67344460d2094fc2cb0a3fe88470c44b019/68747470733a2f2f706f7365722e707567782e6f72672f6d69726f636f772f796969322d6561762f762f756e737461626c65)](https://packagist.org/packages/mirocow/yii2-eav) [![Total Downloads](https://camo.githubusercontent.com/628c9147002e7674f767da6e3a0c04f5b91312091558700c2c03caa629db7cc2/68747470733a2f2f706f7365722e707567782e6f72672f6d69726f636f772f796969322d6561762f646f776e6c6f616473)](https://packagist.org/packages/mirocow/yii2-eav) [![License](https://camo.githubusercontent.com/66a5ae0aea66c6197a08ae6107f5317ead44c22885104d426f18fba811fe2e32/68747470733a2f2f706f7365722e707567782e6f72672f6d69726f636f772f796969322d6561762f6c6963656e7365)](https://packagist.org/packages/mirocow/yii2-eav)[![Join the chat at https://gitter.im/Mirocow/yii2-eav](https://camo.githubusercontent.com/abe08b740a4156153736f791393ec4da6619c4be73212e75769f52edacc0e2b5/68747470733a2f2f6261646765732e6769747465722e696d2f4a6f696e253230436861742e737667)](https://gitter.im/Mirocow/yii2-eav?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

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)

Install
=======

[](#install)

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

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

[](#configure)

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

or

```
php ./yii migrate/up -p=@vendor/mirocow/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' => '@mirocow/eav/messages',
            ],
        ],
    ]
```

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

    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

[](#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' => \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]);
    }
}
```

### View

[](#view)

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

### Form edit

[](#form-edit)

fo load selected field

```
