PHP code example of yarcode / yii2-eav

1. Go to this page and download the library: Download yarcode/yii2-eav library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yarcode / yii2-eav example snippets


/**
 * @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;
}

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

 $form = ActiveForm::begin(); 

$entityName="user_profile";