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

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

yarcode/yii2-eav
================

EAV for Yii2

0.3.2(10y ago)13893MITPHPPHP &gt;=5.4.0

Since Jul 28Pushed 10y ago5 watchersCompare

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

READMEChangelog (6)Dependencies (1)Versions (7)Used By (0)

yii2-eav
========

[](#yii2-eav)

EAV Dynamic Attributes for Yii2

Configuration
-------------

[](#configuration)

Take a look at the *examples/m140423\_034003\_object.php* file and create your own data structure depending on the entity you are using. I the most cases you'll need to change only the $entityName variable. For example:

```
$entityName="user_profile";

```

Extend all models you'll find in the *yarcode\\eav\\models* namespace and place them under the unique namespace. For example *common\\models\\user\_profile\_eav*.

Attach behavior to your model and define the *getEavAttributes* relation:

```
/**
 * @inheritdoc
 */
public function behaviors()
{
    return [
        [
            'class' => \yarcode\eav\EavBehavior::className(),
            'valueClass' => \common\models\user_profile_eav\AttributeValue::className(),
        ],
    ];
}

/**
 * @return yii\db\ActiveQuery
 */
public function getEavAttributes()
{
    $query = \common\models\user_profile_eav\Attribute::find();
    $query->multiple = true;
    return $query;
}
```

Rendering
---------

[](#rendering)

*Controller example*

```
public function actionProfileFields()
{
    $model = Yii::$app->user->identity->profile;
    $model->setScenario(UserProfile::SCENARIO_UPDATE);

    /** @var DynamicModel $eav */
    $eav = $model->getEavModel();

    if ($eav->load(Yii::$app->request->post()) && $eav->validate()) {
        $dbTransaction = Yii::$app->db->beginTransaction();
        try {
            $eav->save(false);
            $dbTransaction->commit();
        } catch (\Exception $e) {
            $dbTransaction->rollBack();
            throw $e;
        }
        return $this->redirect(['index']);
    }

    return $this->render('profile-fields', [
        'model' => $model,
        'eav' => $eav
    ]);
}
```

*View example*

```
