PHP code example of mirocow / yii2-eav
1. Go to this page and download the library: Download mirocow/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/ */
mirocow / yii2-eav example snippets
$attr = new mirocow\eav\models\EavAttribute();
$attr->attributes = [
'entityId' => 1, // Category ID
'typeId' => 1, // ID type from eav_attribute_type
'name' => 'packing', // service name field
'label' => 'Packing', // label text for form
'defaultValue' => '10 kg', // default value
'entityModel' => Product::className(), // work model
' // add rule "
$model = Product::find()->where(['id' => 1])->one();
$model->color = "blue";
$model->packing = "12 kg";
$model->save();
$modules = [
...,
'eav' => [
'class' => 'mirocow\eav\Module',
],
];
php composer.phar
sh
php ./yii migrate/up -p=@mirocow/eav/migrations
sh
php ./yii migrate/up -p=@vendor/mirocow/yii2-eav/src/migrations
php
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
//'basePath' => '@app/messages',
//'sourceLanguage' => 'en-US',
'fileMap' => [
'app' => 'app.php',
'app/error' => 'error.php',
],
],
'eav' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@mirocow/eav/messages',
],
],
]
php
class Product extends \yii\db\ActiveRecord
{
public function rules()
{
return [
[['name'], 'string', 'max' => 255], // Product field
[['c1'], 's' => \mirocow\eav\EavBehavior::className(),
// это модель для таблицы object_attribute_value
'valueClass' => \mirocow\eav\models\EavAttributeValue::className(),
]
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getEavAttributes()
{
return \mirocow\eav\models\EavAttribute::find()
->joinWith('entity')
->where([
'categoryId' => $this->categories[0]->id,
'entityModel' => $this::className()
]);
}
}
php
class Product extends \yii\db\ActiveRecord
{
public function rules()
{
return [
[['name'], 'string', 'max' => 255], // Product field
[['c1'], '=> \mirocow\eav\EavBehavior::className(),
// это модель для таблицы object_attribute_value
'valueClass' => \mirocow\eav\models\EavAttributeValue::className(),
]
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getEavAttributes($attributes = [])
{
return \mirocow\eav\models\EavAttribute::find()
->joinWith('entity')
->where([
//'categoryId' => $this->categories[0]->id,
'entityModel' => $this::className()
])
->orderBy(['order' => SORT_ASC]);
}
}
php
<?=$form->field($model,'test5', ['class' => '\mirocow\eav\widgets\ActiveField'])->eavInput();
php
foreach($model->getEavAttributes()->all() as $attr) {
echo $form->field($model, $attr->name, ['class' => '\mirocow\eav\widgets\ActiveField'])->eavInput();
}
php
foreach($model->getEavAttributes()->orderBy(['order' => SORT_ASC])->all() as $attr) {
echo $form->field($model, $attr->name, ['class' => '\mirocow\eav\widgets\ActiveField'])->eavInput();
}
php
foreach($model->getEavAttributes(['entityId' => 8, 'typeId' => 3])->all() as $attr) {
echo $form->field($model, $attr->name, ['class' => '\mirocow\eav\widgets\ActiveField'])->eavInput();
}
php
<p>
Encode
foreach($model->getEavAttributes()->all() as $attr) {
print_r($model[$attr->name]['value']);
}
php
<?= \mirocow\eav\admin\widgets\Fields::widget([
'model' => $model,
'categoryId' => $model->id,
'entityName' => 'Продукт',
'entityModel' => 'app\models\Product',
])