The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist mazurva/yii2-eav "dev-master"
or add
"mazurva/yii2-eav": "dev-master"
to the require section of your composer.json
file.
php yii migrate/up --migrationPath=@vendor/mazurva/yii2-eav/src/migrations
Attach to your model
use EavTrait; // need for full support label of fields
public function behaviors()
{
return [
'eav' => [
'class' => mazurva\eav\EavBehavior::className(),
'valueClass' => mazurva\eav\models\EavAttributeValue::className(), // this model for table object_attribute_value
]
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getEavAttributes()
{
return $this->hasMany(mazurva\eav\models\EavAttribute::className(), ['categoryId' => 'id']);
}
Sample use in view:
<?=$form->field($model,'eav2'); ?>
or
foreach($model->eavAttributes as $attr){
echo $form->field($model, $attr->name)->textInput();
}
Add new field:
$attr = new mazurva\eav\models\EavAttribute();
$attr->attributes = [
'categoryId' => 1, // Category ID
'name' => 'AttrCategory1', // service name field
'label' => 'Attr1', // label text for form
'defaultValue' => 'attr1', // default value
'entityModel' => SampleModel::className(), // work model
'required'=>false // add rule "required field"
];
$attr->save();