mirocow/yii2-eav
Composer 安装命令:
composer require mirocow/yii2-eav
包简介
EAV for Yii2
关键字:
README 文档
README
Архитектура баз данных EAV(Enity-Attribute-Value, Сущность-Атрибут-Значение)
Screenshots
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 ]; } 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 ]; } 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 '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 'required' => false // add rule "required field" ]; $attr->save(); $attr->attributes = [ 'entityId' => 1, // Category ID 'typeId' => 1, // ID type from eav_attribute_type 'name' => 'color', // service name field 'label' => 'Color', // label text for form 'defaultValue' => 'white', // default value 'entityModel' => Product::className(), // work model 'required' => false // add rule "required field" ]; $attr->save();
Add/Update values
$model = Product::find()->where(['id' => 1])->one(); $model->color = "blue"; $model->packing = "12 kg"; $model->save();
Administrate GUI
Config module EAV for managment of fields
In main config file:
$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', ])?>
mirocow/yii2-eav 适用场景与选型建议
mirocow/yii2-eav 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 7.54k 次下载、GitHub Stars 达 91, 最近一次更新时间为 2015 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「attribute」 「eav」 「fields」 「yii2」 「cck」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mirocow/yii2-eav 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mirocow/yii2-eav 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mirocow/yii2-eav 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
EAV modeling package for Eloquent and Laravel.
Register advanced custom fields with object oriented PHP
A behavior for standard fields logic in CRUD based on kartik dynagrid and detailView
Get wordpress nav menus and share the main site menus with the child sites.
Powerful class discovery system for Laravel with attribute-based scanning, directory traversal, and monorepo support
统计信息
- 总下载量: 7.54k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 96
- 点击次数: 35
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-19