PHPackages                             nullref/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. nullref/yii2-eav

ActiveYii2-extension

nullref/yii2-eav
================

6803[3 issues](https://github.com/NullRefExcep/yii2-eav/issues)PHP

Since Oct 16Pushed 4y ago3 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Yii2 EAV
========

[](#yii2-eav)

[![Latest Stable Version](https://camo.githubusercontent.com/5d66682947e1d268e811784ca8e98d2167fe7d208686c5314960326a1e5f9660/68747470733a2f2f706f7365722e707567782e6f72672f6e756c6c7265662f796969322d6561762f762f737461626c65)](https://packagist.org/packages/nullref/yii2-eav) [![Total Downloads](https://camo.githubusercontent.com/334a04ea48a8edef6ffc9205878b19ca741b2f78afdc5e27f37994734979ebd7/68747470733a2f2f706f7365722e707567782e6f72672f6e756c6c7265662f796969322d6561762f646f776e6c6f616473)](https://packagist.org/packages/nullref/yii2-eav) [![Latest Unstable Version](https://camo.githubusercontent.com/885dd7311469aafc52daef1b8a4a139cbe6132274237fa8c25d92862615e6ba5/68747470733a2f2f706f7365722e707567782e6f72672f6e756c6c7265662f796969322d6561762f762f756e737461626c65)](https://packagist.org/packages/nullref/yii2-eav) [![License](https://camo.githubusercontent.com/cdadac670307b535406f4335821f7e3f07788638e4a1816871f44e830a7d9a83/68747470733a2f2f706f7365722e707567782e6f72672f6e756c6c7265662f796969322d6561762f6c6963656e7365)](https://packagist.org/packages/nullref/yii2-eav)

WIP

Module for EAV (entity attribute value) anti pattern

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

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

```

or add

```
"nullref/yii2-eav": "*"

```

to the require section of your `composer.json` file.

Then You have run console command for install this module and run migrations:

```
php yii module/install nullref/yii2-eav

```

Pay attention that if you don't use our [application template](https://github.com/NullRefExcep/yii2-boilerplate)it needs to change config files structure to have ability run commands that show above.

Please check this [documentation section](https://github.com/NullRefExcep/yii2-core#config-structure)

Setup
-----

[](#setup)

Add behavior to target model

```
use nullref\eav\behaviors\Entity;
use nullref\eav\models\attribute\Set;
use nullref\eav\models\Entity as EntityModel;

/**
 * ...
 * @property EntityModel $eav
 * ...
 */
class Product extends \yii\db\ActiveRecord
    //...
    public function behaviors()
    {
        return [
            /** ... **/
            'eav' => [
                'class' => Entity::class,
                'entity' => function () {
                    return new EntityModel([
                        'sets' => [
                            Set::findOne(['code' => 'product']), //product -- set from db
                        ],
                    ]);
                },
            ],
        ];
    }
    //...
}
```

Create set and attribute for it in admin panel

Add attributes widget to entity edit form

```

```

If you need some dynamic configuration sets of your model you can use method `afterFind()`:

```
public function afterFind()
{
    $this->attachBehavior('eav', [
        'class' => Entity::class,
        'entity' => function () {
            $setIds = $this->getCategories()->select('set_id')->column();
            $setIds[] = Set::findOne(['code' => 'product'])->id;
            return new EntityModel([
                'sets' => Set::findAll(['id' => array_unique($setIds)]),
            ]);
        },
    ]);
    parent::afterFind();
}
```

In above example we have many-to-many relation product model with category which has set\_id column.

Pay attention that this example could caused n+1 query problem. To prevent this problem use query caching or memoization. For example, change:

```
\nullref\eav\models\attribute\Set::findOne(['code' => 'product']),
```

to

```
\nullref\useful\helpers\Memoize::call([Set::class, 'findOne'],[['code' => 'product']]),
```

Using in search model
---------------------

[](#using-in-search-model)

If you need filtering your records by eav fields you need to modify `YourModelSearch::search()` method by following code:

```
    //...
    if (!$this->validate()) {
        return $dataProvider;
    }
    //...
    foreach ($this->eav->getAttributes() as $key => $value) {

        $valueModel = $this->eav->getAttributeModel($key)->createValue();

        $valueModel->setScenario('search');
        $valueModel->load(['value' => $value], '');
        if ($valueModel->validate(['value'])) {
            $valueModel->addJoin($query, self::tableName());
            $valueModel->addWhere($query);
        }
    }
    //...
    return $dataProvider;
```

To output columns in gridview use `nullref\eav\helpers\Grid::getGridColumns()`:

```
