yii2-eav EAV Dynamic Attributes for Yii2

EAV Dynamic Attributes for Yii2

See github https://github.com/Mirocow/yii2-eav

========

АрхитСктура Π±Π°Π· Π΄Π°Π½Π½Ρ‹Ρ… EAV(Enity-Attribute-Value, Π‘ΡƒΡ‰Π½ΠΎΡΡ‚ΡŒ-Атрибут-Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅)

Latest Stable Version Latest Unstable Version Total Downloads License Join the chat at https://gitter.im/Mirocow/yii2-eav

Screenshots ΒΆ

  1. Edit attributes
  2. Edit form
  3. Add github repository
  4. Configure
  5. Use
  6. Administrate GUI
  7. Config module EAV for managment of fields
  8. Form
  9. Add / Edit attribute

Edit attributes ΒΆ

List of attributes ΒΆ

Edit attribute ΒΆ

Edit form ΒΆ

Install ΒΆ

Add github repository ΒΆ

		"repositories": [
				{
						"type": "git",
						"url": "https://github.com/mirocow/yii2-eav.git"
				}
		]

and then

php composer.phar require --prefer-dist "mirocow/yii2-eav" "*"

Configure ΒΆ

php ./yii migrate/up -p=@mirocow/eav/migrations

or

php ./yii migrate/up -p=@vendor/mirocow/yii2-eav/src/migrations

and then add messages settings

		'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',
						],
				],
		]

Use ΒΆ

Model ΒΆ
Simple ΒΆ
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' => \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()
				]);
		}

}
Advanced ΒΆ
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' => \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]);
		}

}
View ΒΆ

Insert this code for create widget or load all EAV inputs fields for model

Form edit ΒΆ

fo load selected field

		<?=$form->field($model,'test5', ['class' => '\mirocow\eav\widgets\ActiveField'])->eavInput(); ?>

or for load all fields

Simple ΒΆ
		<?php
		foreach($model->getEavAttributes()->all() as $attr){
				echo $form->field($model, $attr->name, ['class' => '\mirocow\eav\widgets\ActiveField'])->eavInput();
		}
		?>

or add sorted

		<?php
		foreach($model->getEavAttributes()->orderBy(['order' => SORT_ASC])->all() as $attr){
				echo $form->field($model, $attr->name, ['class' => '\mirocow\eav\widgets\ActiveField'])->eavInput();
		}
		?>
Advanced ΒΆ
		<?php
		foreach($model->getEavAttributes(['entityId' => 8, 'typeId' => 3])->all() as $attr){
				echo $form->field($model, $attr->name, ['class' => '\mirocow\eav\widgets\ActiveField'])->eavInput();
		}
		?>
Partial template ΒΆ
<p>
Encode

<?php
	foreach($model->getEavAttributes()->all() as $attr){
		print_r($model[$attr->name]['value']);
	}
?>
</p>

<p>
String

<?php
	foreach($model->getEavAttributes()->all() as $attr){
		echo $model[$attr->name];
	}
?>
Add attribute ΒΆ
$attr = new mirocow\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: `php $modules = [

	...,
	'eav' => [
			'class' => 'mirocow\eav\Module',
	],

]; `

Form ΒΆ

Add / Edit attribute ΒΆ

<?= \mirocow\eav\admin\widgets\Fields::widget([
		'model' => $model,
		'categoryId' => $model->id,
		'entityName' => 'ΠŸΡ€ΠΎΠ΄ΡƒΠΊΡ‚',
		'entityModel' => 'app\models\Product',
])?>
1 0
3 followers
0 downloads
Yii Version: 2.0
License: BSD-2-Clause
Category: User Interface
Developed by: Mirocow Mirocow
Created on: Oct 20, 2015
Last updated: 9 years ago

Related Extensions