blacksesion / yii2-eav
为 Yii2 的 EAV
1.1.3
2018-04-06 17:35 UTC
Requires
- php: >=5.5.0
- yiisoft/yii2: *
README
EAV(实体-属性-值) 源自 mirocow 的工作
屏幕截图
编辑属性
属性列表
编辑属性
编辑表单
活动字段规则
安装
添加 github 仓库
"repositories": [ { "type": "git", "url": "https://github.com/blacksesion/yii2-eav.git" } ]
然后
php composer.phar require --prefer-dist "blacksesion/yii2-eav" "*"
配置
php ./yii migrate/up -p=@blacksesion/eav/migrations
或者
php ./yii migrate/up -p=@vendor/blacksesion/yii2-eav/src/migrations
使用
模型
简单
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() { return \blacksesion\eav\models\EavAttribute::find() ->joinWith('entity') ->where([ 'categoryId' => $this->categories[0]->id, 'entityModel' => $this::className() ]); } }
高级
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(); ?>
或者用于加载所有字段
简单
<?php foreach($model->getEavAttributes()->all() as $attr){ echo $form->field($model, $attr->name, ['class' => '\blacksesion\eav\widgets\ActiveField'])->eavInput(); } ?>
或者添加排序
<?php foreach($model->getEavAttributes()->orderBy(['order' => SORT_ASC])->all() as $attr){ echo $form->field($model, $attr->name, ['class' => '\blacksesion\eav\widgets\ActiveField'])->eavInput(); } ?>
高级
<?php foreach($model->getEavAttributes(['entityId' => 8, 'typeId' => 3])->all() as $attr){ echo $form->field($model, $attr->name, ['class' => '\blacksesion\eav\widgets\ActiveField'])->eavInput(); } ?>
部分模板
<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]; } ?>
添加属性
$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();
管理 GUI
配置 EAV 模块以管理字段
在主配置文件中
$modules = [ ..., 'eav' => [ 'class' => 'blacksesion\eav\Module', ], ];
表单
添加/编辑属性
<?= \blacksesion\eav\admin\widgets\Fields::widget([ 'model' => $model, 'categoryId' => $model->id, 'entityName' => 'Entidad', 'entityModel' => 'common\models\Product', ])?>