blacksesion/yii2-eav 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

blacksesion/yii2-eav

最新稳定版本:1.1.3

Composer 安装命令:

composer require blacksesion/yii2-eav

包简介

EAV para Yii2

README 文档

README

EAV(Enity-Attribute-Value) Tomado del trabajo de mirocow

Screenshots

Edit attributes

List of attributes

Edit attribute

Edit form

Active field rules

Install

Add github repository

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

and then

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

Configure

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

or

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

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

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' => \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

Simple

    <?php
    foreach($model->getEavAttributes()->all() as $attr){
        echo $form->field($model, $attr->name, ['class' => '\blacksesion\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' => '\blacksesion\eav\widgets\ActiveField'])->eavInput();
    }
    ?>

Advanced

    <?php
    foreach($model->getEavAttributes(['entityId' => 8, 'typeId' => 3])->all() as $attr){
        echo $form->field($model, $attr->name, ['class' => '\blacksesion\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 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:

$modules = [
    ...,
    'eav' => [
        'class' => 'blacksesion\eav\Module',
    ],
];

Form

Add / Edit attribute

<?= \blacksesion\eav\admin\widgets\Fields::widget([
                      'model' => $model,
                      'categoryId' => $model->id,
                      'entityName' => 'Entidad',
                      'entityModel' => 'common\models\Product',
                  ])?>

统计信息

  • 总下载量: 1.63k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 57
  • 开发语言: JavaScript

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-09-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固