Преглед изворни кода

fix: 修复命名错误eva改为eav

runphp пре 6 месеци
родитељ
комит
489971d003

+ 2 - 15
database/migrations/20250712021908_extension_eva_entity_type.php → database/migrations/20250712021908_extension_eav_entity_type.php

@@ -2,7 +2,7 @@
 
 use think\migration\Migrator;
 
-class ExtensionEvaEntityType extends Migrator
+class ExtensionEavEntityType extends Migrator
 {
     /**
      * Change Method.
@@ -11,23 +11,10 @@ class ExtensionEvaEntityType extends Migrator
      *
      * More information on writing migrations is available here:
      * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
-     *
-     * The following commands can be used in this method and Phinx will
-     * automatically reverse them when rolling back:
-     *
-     *    createTable
-     *    renameTable
-     *    addColumn
-     *    renameColumn
-     *    addIndex
-     *    addForeignKey
-     *
-     * Remember to call "create()" or "update()" and NOT "save()" when working
-     * with the Table class.
      */
     public function change(): void
     {
-        $table = $this->table('extension_eva_entity_type', ['engine' => 'InnoDB', 'comment' => '实体类型表(如商品/用户等)']);
+        $table = $this->table('extension_eav_entity_type', ['engine' => 'InnoDB', 'comment' => '实体类型表(如商品/用户等)']);
         $table->addColumn('entity_type_code', 'string', ['limit' => 50, 'comment' => '实体类型编码'])
             ->addColumn('entity_table', 'string', ['limit' => 64, 'comment' => '实体主表名'])
             ->addIndex(['entity_type_code'], ['name' => 'entity_type_code', 'unique' => true])

+ 3 - 20
database/migrations/20250712021931_extension_eva_attribute.php → database/migrations/20250712021931_extension_eav_attribute.php

@@ -2,33 +2,17 @@
 
 use think\migration\Migrator;
 
-class ExtensionEvaAttribute extends Migrator
+class ExtensionEavAttribute extends Migrator
 {
     /**
      * Change Method.
      *
      * Write your reversible migrations using this method.
-     *
-     * More information on writing migrations is available here:
-     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
-     *
-     * The following commands can be used in this method and Phinx will
-     * automatically reverse them when rolling back:
-     *
-     *    createTable
-     *    renameTable
-     *    addColumn
-     *    renameColumn
-     *    addIndex
-     *    addForeignKey
-     *
-     * Remember to call "create()" or "update()" and NOT "save()" when working
-     * with the Table class.
      */
     public function change(): void
     {
-        $this->table('extension_eva_attribute', ['engine' => 'InnoDB', 'charset' => 'utf8mb4', 'comment' => '属性定义表'])
-            ->addColumn('entity_type_id', 'integer', ['comment' => '关联实体类型'])
+        $this->table('extension_eav_attribute', ['engine' => 'InnoDB', 'charset' => 'utf8mb4', 'comment' => '属性定义表'])
+            ->addColumn('entity_type_id', 'integer', ['signed' => false, 'comment' => '关联实体类型'])
             ->addColumn('attribute_code', 'string', ['comment' => '属性编码', 'limit' => 64])
             ->addColumn('backend_type', 'string', ['comment' => '存储类型(int/varchar/decimal/text), 需与frontend_input匹配', 'limit' => 8])
             ->addColumn('frontend_input', 'string', ['comment' => '表单控件类型', 'limit' => 16])
@@ -38,6 +22,5 @@ class ExtensionEvaAttribute extends Migrator
             ->addTimestamps()
             ->addSoftDelete()
             ->create();
-
     }
 }

+ 6 - 4
database/migrations/20250712022607_extension_eva_value.php → database/migrations/20250712022607_extension_eav_value.php

@@ -3,7 +3,7 @@
 use think\migration\Migrator;
 use think\migration\db\Column;
 
-class ExtensionEvaValue extends Migrator
+class ExtensionEavValue extends Migrator
 {
     /**
      * Change Method.
@@ -28,16 +28,18 @@ class ExtensionEvaValue extends Migrator
      */
     public function change(): void
     {
-        $table = $this->table('extension_eva_value', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '扩展:EAV值表']);
+        $table = $this->table('extension_eav_value', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '扩展:EAV值表']);
         $table
             ->addColumn('entity_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '实体ID'])
-            ->addColumn('attribute_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '属性ID'])
+            ->addColumn('attribute_id', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '属性ID'])
+            ->addColumn('entity_type_id', 'integer', ['signed' => false, 'default' => 0, 'comment' => '实体类型ID'])
             ->addColumn('value_int', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '整数值'])
             ->addColumn('value_varchar', 'string', ['limit' => 255, 'default' => '', 'comment' => '字符串值'])
             ->addColumn('value_decimal', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0.00, 'comment' => '数值值'])
             ->addColumn('value_text', 'text', ['comment' => '文本值'])
             ->addColumn('value_datetime', 'datetime', ['comment' => '日期时间值'])
-            ->addIndex(['entity_id', 'attribute_id'], ['name' => 'entity_id_attribute_id', 'unique' => true])
+            ->addIndex(['entity_id', 'attribute_id'], ['name' => 'entity_id_attribute_id'])
+            ->addIndex(['entity_id', 'entity_type_id'],['name' => 'idx_entity_id_entity_type_id'])
             ->addTimestamps()
             ->addSoftDelete()
             ->create();

+ 25 - 0
database/migrations/20250712025652_extension_eav_attribute_option.php

@@ -0,0 +1,25 @@
+<?php
+
+use think\migration\Migrator;
+use think\migration\db\Column;
+
+class ExtensionEavAttributeOption extends Migrator
+{
+    /**
+     * Change Method.
+     *
+     * Write your reversible migrations using this method.
+     */
+    public function change()
+    {
+        $table = $this->table('extension_eav_attribute_option', ['engine' => 'InnoDB', 'comment' => '属性选项表']);
+        $table->addColumn(Column::unsignedInteger('attribute_id')->setComment('属性ID'))
+            ->addColumn(Column::string('label')->setComment('显示文本'))
+            ->addColumn(Column::string('value')->setComment('选项值'))
+            ->addColumn(Column::unsignedInteger('sort')->setComment('排序'))
+            ->addTimestamps()
+            ->addSoftDelete()
+            ->addIndex(['attribute_id'])
+            ->create();
+    }
+}

+ 0 - 41
database/migrations/20250712025652_extension_eva_attribute_option.php

@@ -1,41 +0,0 @@
-<?php
-
-use think\migration\Migrator;
-use think\migration\db\Column;
-
-class ExtensionEvaAttributeOption extends Migrator
-{
-    /**
-     * Change Method.
-     *
-     * Write your reversible migrations using this method.
-     *
-     * More information on writing migrations is available here:
-     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
-     *
-     * The following commands can be used in this method and Phinx will
-     * automatically reverse them when rolling back:
-     *
-     *    createTable
-     *    renameTable
-     *    addColumn
-     *    renameColumn
-     *    addIndex
-     *    addForeignKey
-     *
-     * Remember to call "create()" or "update()" and NOT "save()" when working
-     * with the Table class.
-     */
-    public function change()
-    {
-        $table = $this->table('extension_eva_attribute_option', ['engine' => 'InnoDB', 'comment' => '属性选项表']);
-        $table->addColumn(Column::integer('attribute_id')->setComment('属性ID'))
-            ->addColumn(Column::string('label')->setComment('显示文本'))
-            ->addColumn(Column::string('value')->setComment('选项值'))
-            ->addColumn(Column::integer('sort')->setComment('排序'))
-            ->addTimestamps()
-            ->addSoftDelete()
-            ->addIndex(['attribute_id'])
-            ->create();
-    }
-}

+ 0 - 37
database/migrations/20250908154056_eav_value_entity_type_id.php

@@ -1,37 +0,0 @@
-<?php
-
-use think\migration\Migrator;
-use think\migration\db\Column;
-
-class EavValueEntityTypeId extends Migrator
-{
-    /**
-     * Change Method.
-     *
-     * Write your reversible migrations using this method.
-     *
-     * More information on writing migrations is available here:
-     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
-     *
-     * The following commands can be used in this method and Phinx will
-     * automatically reverse them when rolling back:
-     *
-     *    createTable
-     *    renameTable
-     *    addColumn
-     *    renameColumn
-     *    addIndex
-     *    addForeignKey
-     *
-     * Remember to call "create()" or "update()" and NOT "save()" when working
-     * with the Table class.
-     */
-    public function change()
-    {
-
-        $table = $this->table('extension_eva_value');
-        $table->addColumn('entity_type_id', 'integer', ['signed' => false, 'default' => 0, 'comment' => '实体类型ID', 'after' => 'attribute_id'])
-            ->addIndex(['entity_id', 'entity_type_id'],['name' => 'idx_entity_id_entity_type_id'])
-            ->update();
-    }
-}

+ 5 - 5
src/Controller/AttributeOptionController.php

@@ -4,7 +4,7 @@ namespace SixShop\Eav\Controller;
 
 
 use SixShop\Core\Request;
-use SixShop\Eav\Entity\EvaAttributeOptionEntity;
+use SixShop\Eav\Entity\EavAttributeOptionEntity;
 use think\Response;
 
 
@@ -12,22 +12,22 @@ use function SixShop\Core\success_response;
 
 class AttributeOptionController
 {
-    public function index(Request $request, EvaAttributeOptionEntity $entity): Response
+    public function index(Request $request, EavAttributeOptionEntity $entity): Response
     {
         $attributeId = $request->get('attribute_id/d');
         return success_response($entity->where(['attribute_id' =>$attributeId])->select());
     }
 
-    public function save(Request $request, EvaAttributeOptionEntity $entity): Response
+    public function save(Request $request, EavAttributeOptionEntity $entity): Response
     {
         $data = $request->post();
         return success_response($entity->save($data));
     }
-    public function update(int $id, Request $request, EvaAttributeOptionEntity $entity): Response
+    public function update(int $id, Request $request, EavAttributeOptionEntity $entity): Response
     {
         return success_response($entity->update($request->post(), ['id' => $id]));
     }
-    public function delete(int $id, EvaAttributeOptionEntity $entity): Response
+    public function delete(int $id, EavAttributeOptionEntity $entity): Response
     {
         return success_response($entity->destroy($id));
     }

+ 5 - 5
src/Controller/EntityAttributeController.php

@@ -3,20 +3,20 @@ declare(strict_types=1);
 namespace SixShop\Eav\Controller;
 
 use SixShop\Core\Request;
-use SixShop\Eav\Entity\EvaAttributeEntity;
+use SixShop\Eav\Entity\EavAttributeEntity;
 use think\Response;
 
 use function SixShop\Core\success_response;
 
 class EntityAttributeController
 {
-    public function index(Request $request, EvaAttributeEntity $entity): Response
+    public function index(Request $request, EavAttributeEntity $entity): Response
     {
         $entityTypeID = $request->get('entity_type_id/d');
         return success_response($entity->where('entity_type_id', $entityTypeID)->select());
     }
 
-    public function save(Request $request, EvaAttributeEntity $entity): Response
+    public function save(Request $request, EavAttributeEntity $entity): Response
     {
         $data = $request->post();
         $entityObject = $entity->withTrashed()->where([
@@ -30,13 +30,13 @@ class EntityAttributeController
         return success_response($entityObject->save($data));
     }
 
-    public function update(int $id, Request $request, EvaAttributeEntity $entity): Response
+    public function update(int $id, Request $request, EavAttributeEntity $entity): Response
     {
         $data = $request->post();
         return success_response($entity->where('id', $id)->update($data));
     }
 
-    public function delete(int $id, EvaAttributeEntity $entity): Response
+    public function delete(int $id, EavAttributeEntity $entity): Response
     {
         return success_response($entity->destroy($id));
     }

+ 6 - 6
src/Controller/EntityTypeController.php

@@ -3,23 +3,23 @@ declare(strict_types=1);
 namespace SixShop\Eav\Controller;
 
 use SixShop\Core\Request;
-use SixShop\Eav\Entity\EvaEntityTypeEntity;
+use SixShop\Eav\Entity\EavEntityTypeEntity;
 use think\Response;
 
 use function SixShop\Core\success_response;
 
 class EntityTypeController
 {
-    public function index(EvaEntityTypeEntity $entityTypeEntity): Response
+    public function index(EavEntityTypeEntity $entityTypeEntity): Response
     {
         return success_response($entityTypeEntity->select());
     }
 
-    public function read(int $id, EvaEntityTypeEntity $entity): Response
+    public function read(int $id, EavEntityTypeEntity $entity): Response
     {
         return success_response($entity->find($id));
     }
-    public function save(Request $request, EvaEntityTypeEntity $entity): Response
+    public function save(Request $request, EavEntityTypeEntity $entity): Response
     {
         $data = $request->post();
         $entityObject = $entity->withTrashed()
@@ -32,11 +32,11 @@ class EntityTypeController
         return success_response($entityObject->save($data));
     }
 
-    public function update(int $id, Request $request, EvaEntityTypeEntity $entity): Response
+    public function update(int $id, Request $request, EavEntityTypeEntity $entity): Response
     {
         return success_response($entity->update($request->post(), ['id' => $id]));
     }
-    public function delete(int $id, EvaEntityTypeEntity $entity): Response
+    public function delete(int $id, EavEntityTypeEntity $entity): Response
     {
         return success_response($entity->destroy($id));
     }

+ 9 - 7
src/Controller/ValueController.php

@@ -4,8 +4,8 @@ declare(strict_types=1);
 namespace SixShop\Eav\Controller;
 
 use SixShop\Core\Request;
-use SixShop\Eav\Entity\EvaValueEntity;
-use SixShop\Eav\Model\EvaAttributeModel;
+use SixShop\Eav\Entity\EavValueEntity;
+use SixShop\Eav\Model\EavAttributeModel;
 use think\Response;
 
 use function SixShop\Core\error_response;
@@ -17,7 +17,7 @@ class ValueController
      * 查询指定实体的所有属性值
      * GET /eav/value?entity_id=123&entity_type_id=1
      */
-    public function index(Request $request, EvaValueEntity $entity): Response
+    public function index(Request $request, EavValueEntity $entity): Response
     {
         $entityId = $request->get('entity_id/d');
         $entityTypeId = $request->get('entity_type_id/d');
@@ -31,7 +31,9 @@ class ValueController
         $list = $entity->where([
             'entity_id' => $entityId,
             'entity_type_id' => $entityTypeId,
-        ])->select();
+        ])->with(['attribute' => function ($query) {
+            $query->with(['options']);
+        }])->select();
         return success_response($list);
     }
 
@@ -42,7 +44,7 @@ class ValueController
      *   { "entity_id": 123, "entity_type_id": 1, "attribute_id": 2, "value": "红色" }
      * ]
      */
-    public function save(Request $request, EvaValueEntity $entity): Response
+    public function save(Request $request, EavValueEntity $entity): Response
     {
         $data = $request->post();
         if (empty($data) || !is_array($data)) {
@@ -59,7 +61,7 @@ class ValueController
                 continue;
             }
             // 获取属性类型
-            $attr = EvaAttributeModel::find($item['attribute_id']);
+            $attr = EavAttributeModel::find($item['attribute_id']);
             if (!$attr) {
                 continue;
             }
@@ -101,7 +103,7 @@ class ValueController
      * 删除指定实体的所有属性值
      * DELETE /eav/value?entity_id=123&entity_type_id=1
      */
-    public function delete(Request $request, EvaValueEntity $entity): Response
+    public function delete(Request $request, EavValueEntity $entity): Response
     {
         $entityId = $request->get('entity_id/d');
         $entityTypeId = $request->get('entity_type_id/d');

+ 2 - 2
src/Entity/EvaAttributeEntity.php → src/Entity/EavAttributeEntity.php

@@ -1,10 +1,10 @@
 <?php
 declare(strict_types=1);
+
 namespace SixShop\Eav\Entity;
 
 use SixShop\Core\Entity\BaseEntity;
 
-class EvaAttributeEntity extends BaseEntity
+class EavAttributeEntity extends BaseEntity
 {
-
 }

+ 2 - 1
src/Entity/EvaAttributeOptionEntity.php → src/Entity/EavAttributeOptionEntity.php

@@ -1,10 +1,11 @@
 <?php
 declare(strict_types=1);
+
 namespace SixShop\Eav\Entity;
 
 use SixShop\Core\Entity\BaseEntity;
 
-class EvaAttributeOptionEntity extends BaseEntity
+class EavAttributeOptionEntity extends BaseEntity
 {
 
 }

+ 17 - 0
src/Entity/EavEntityTypeEntity.php

@@ -0,0 +1,17 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\Eav\Entity;
+
+use SixShop\Eav\Model\EavEntityTypeModel;
+use think\Entity;
+
+class EavEntityTypeEntity extends Entity
+{
+    protected function getOptions(): array
+    {
+        return [
+            'modelClass' => EavEntityTypeModel::class,
+        ];
+    }
+}

+ 1 - 1
src/Entity/EvaValueEntity.php → src/Entity/EavValueEntity.php

@@ -5,7 +5,7 @@ namespace SixShop\Eav\Entity;
 
 use SixShop\Core\Entity\BaseEntity;
 
-class EvaValueEntity extends BaseEntity
+class EavValueEntity extends BaseEntity
 {
 
 }

+ 0 - 16
src/Entity/EvaEntityTypeEntity.php

@@ -1,16 +0,0 @@
-<?php
-
-namespace SixShop\Eav\Entity;
-
-use SixShop\Eav\Model\EvaEntityTypeModel;
-use think\Entity;
-
-class EvaEntityTypeEntity extends Entity
-{
-    protected function getOptions(): array
-    {
-        return [
-            'modelClass' => EvaEntityTypeModel::class,
-        ];
-    }
-}

+ 15 - 5
src/Model/EvaAttributeModel.php → src/Model/EavAttributeModel.php

@@ -4,9 +4,10 @@ namespace SixShop\Eav\Model;
 
 use think\Model;
 use think\model\concern\SoftDelete;
+use think\model\relation\HasMany;
 
 /**
- * Class SixShop\Eav\Model\ExtensionEvaAttributeModel
+ * Class SixShop\Eav\Model\EavAttributeModel
  *
  * @property bool $is_required 是否必填
  * @property int $entity_type_id 关联实体类型
@@ -21,9 +22,18 @@ use think\model\concern\SoftDelete;
  * @method static \think\db\Query onlyTrashed()
  * @method static \think\db\Query withTrashed()
  */
-class EvaAttributeModel extends Model
+class EavAttributeModel extends Model
 {
-    protected $name = 'extension_eva_attribute';
-    protected $pk = 'id';
     use SoftDelete;
-}
+    protected function getOptions(): array
+    {
+        return [
+            'name' => 'extension_eav_attribute'
+        ];
+    }
+
+    public function options(): HasMany
+    {
+        return $this->hasMany(EavAttributeOptionModel::class, 'attribute_id', 'id');
+    }
+}

+ 10 - 5
src/Model/EvaAttributeOptionModel.php → src/Model/EavAttributeOptionModel.php

@@ -7,7 +7,7 @@ use think\Model;
 use think\model\concern\SoftDelete;
 
 /**
- * Class SixShop\Eav\Model\ExtensionEvaAttributeOptionModel
+ * Class SixShop\Eav\Model\EavAttributeOptionModel
  *
  * @property int $attribute_id 属性ID
  * @property int $id
@@ -20,9 +20,14 @@ use think\model\concern\SoftDelete;
  * @method static \think\db\Query onlyTrashed()
  * @method static \think\db\Query withTrashed()
  */
-class EvaAttributeOptionModel extends Model
+class EavAttributeOptionModel extends Model
 {
-    protected $name = 'extension_eva_attribute_option';
-    protected $pk = 'id';
     use SoftDelete;
-}
+
+    protected function getOptions(): array
+    {
+        return [
+            'name' => 'extension_eav_attribute_option',
+        ];
+    }
+}

+ 9 - 5
src/Model/EvaEntityTypeModel.php → src/Model/EavEntityTypeModel.php

@@ -6,7 +6,7 @@ use think\Model;
 use think\model\concern\SoftDelete;
 
 /**
- * Class SixShop\Eav\Model\ExtensionEvaEntityTypeModel
+ * Class SixShop\Eav\Model\EavEntityTypeModel
  *
  * @property int $id
  * @property string $create_time
@@ -17,9 +17,13 @@ use think\model\concern\SoftDelete;
  * @method static \think\db\Query onlyTrashed()
  * @method static \think\db\Query withTrashed()
  */
-class EvaEntityTypeModel extends Model
+class EavEntityTypeModel extends Model
 {
-    protected $name = 'extension_eva_entity_type';
-    protected $pk = 'id';
     use SoftDelete;
-}
+    protected function getOptions(): array
+    {
+        return [
+            'name' => 'extension_eav_entity_type'
+        ];
+    }
+}

+ 15 - 5
src/Model/EvaValueModel.php → src/Model/EavValueModel.php

@@ -4,9 +4,10 @@ namespace SixShop\Eav\Model;
 
 use think\Model;
 use think\model\concern\SoftDelete;
+use think\model\relation\BelongsTo;
 
 /**
- * Class SixShop\Eav\Model\ExtensionEvaValueModel
+ * Class SixShop\Eav\Model\EavValueModel
  *
  * @property float $value_decimal 数值值
  * @property int $attribute_id 属性ID
@@ -22,9 +23,18 @@ use think\model\concern\SoftDelete;
  * @method static \think\db\Query onlyTrashed()
  * @method static \think\db\Query withTrashed()
  */
-class EvaValueModel extends Model
+class EavValueModel extends Model
 {
-    protected $name = 'extension_eva_value';
-    protected $pk = 'id';
     use SoftDelete;
-}
+    protected function getOptions(): array
+    {
+        return [
+            'name' => 'extension_eav_value'
+        ];
+    }
+
+    public function attribute(): BelongsTo
+    {
+        return $this->belongsTo(EavAttributeModel::class, 'attribute_id', 'id');
+    }
+}