EAV Atributos Dinamicos para Yii2
EAV(Enity-Attribute-Value)
Tomado del trabajo de mirocow, (*1)
Screenshots
Edit attributes
List of attributes
, (*2)
Edit attribute
, (*3)
, (*4)
Active field rules
, (*5)
Install
Add github repository
"repositories": [
{
"type": "git",
"url": "https://github.com/blacksesion/yii2-eav.git"
}
]
and then, (*6)
php composer.phar require --prefer-dist "blacksesion/yii2-eav" "*"
``` sh
php ./yii migrate/up -p=@blacksesion/eav/migrations, (*7)
or
``` sh
php ./yii migrate/up -p=@vendor/blacksesion/yii2-eav/src/migrations
Use
Model
Simple
``` php
class Product extends \yii\db\ActiveRecord
{, (*8)
/**
*
*
*/
public function rules()
{
return [
[['name'], 'string', 'max' => 255], // Product field
[['c1'], 'required'], // Attribute field
[['c1'], 'string', 'max' => 255], // Attribute field
];
}
/**
* create_time, update_time to now()
* crate_user_id, update_user_id to current login user id
*/
public function behaviors()
{
return [
'eav' => [
'class' => \blacksesion\eav\EavBehavior::className(),
'valueClass' => \blacksesion\eav\models\EavAttributeValue::className(),
]
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getEavAttributes()
{
return \blacksesion\eav\models\EavAttribute::find()
->joinWith('entity')
->where([
'categoryId' => $this->categories[0]->id,
'entityModel' => $this::className()
]);
}
}, (*9)
#### Advanced
``` php
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
];
}
/**
* create_time, update_time to now()
* crate_user_id, update_user_id to current login user id
*/
public function behaviors()
{
return [
'eav' => [
'class' => \blacksesion\eav\EavBehavior::className(),
'valueClass' => \blacksesion\eav\models\EavAttributeValue::className(),
]
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getEavAttributes($attributes = [])
{
return \blacksesion\eav\models\EavAttribute::find()
->joinWith('entity')
->where([
//'categoryId' => $entityId,
'entityModel' => $this::className()
])->andWhere($attributes);
}
}
### View
Insert this code for create widget or load all EAV inputs fields for model
### Form edit
fo load selected field
``` php
<?=$form->field($model,'test5', ['class' => '\blacksesion\eav\widgets\ActiveField'])->eavInput(); ?>
or for load all fields, (*10)
Simple
``` php
getEavAttributes()->all() as $attr){
echo $form->field($model, $attr->name, ['class' => '\blacksesion\eav\widgets\ActiveField'])->eavInput();
}
?>, (*11)
or add sorted
``` php
<?php
foreach($model->getEavAttributes()->orderBy(['order' => SORT_ASC])->all() as $attr){
echo $form->field($model, $attr->name, ['class' => '\blacksesion\eav\widgets\ActiveField'])->eavInput();
}
?>
Advanced
``` php
getEavAttributes(['entityId' => 8, 'typeId' => 3])->all() as $attr){
echo $form->field($model, $attr->name, ['class' => '\blacksesion\eav\widgets\ActiveField'])->eavInput();
}
?>, (*12)
### Partial template
``` php
Encode
getEavAttributes()->all() as $attr){
print_r($model[$attr->name]['value']);
}
?>
, (*13)
<
p>
String
getEavAttributes()->all() as $attr){
echo $model[$attr->name];
}
?>
Add attribute
$attr = new blacksesion\eav\models\EavAttribute();
$attr->attributes = [
'entityId' => 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();
Administrate GUI
Config module EAV for managment of fields
In main config file:, (*14)
$modules = [
...,
'eav' => [
'class' => 'blacksesion\eav\Module',
],
];
Add / Edit attribute
php
<?= \blacksesion\eav\admin\widgets\Fields::widget([
'model' => $model,
'categoryId' => $model->id,
'entityName' => 'Entidad',
'entityModel' => 'common\models\Product',
])?>
, (*15)