Kaynağa Gözat

fix: Value模型添加实体类型ID Add entity_type_id to Value model and related APIs

runphp 6 ay önce
ebeveyn
işleme
ef99368a07

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

@@ -0,0 +1,37 @@
+<?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();
+    }
+}

+ 12 - 0
route/api.php

@@ -0,0 +1,12 @@
+<?php
+declare(strict_types= 1);
+
+use SixShop\Eav\Controller\AttributeOptionController;
+use SixShop\Eav\Controller\EntityAttributeController;
+use SixShop\Eav\Controller\ValueController;
+use think\facade\Route;
+
+
+Route::get('entity_attribute', [EntityAttributeController::class, 'index']);
+Route::get('attribute_option', [AttributeOptionController::class, 'index']);
+Route::get('value', [ValueController::class, 'index']);

+ 15 - 6
src/Controller/ValueController.php

@@ -15,17 +15,22 @@ class ValueController
 {
     /**
      * 查询指定实体的所有属性值
-     * GET /eav/value?entity_id=123
+     * GET /eav/value?entity_id=123&entity_type_id=1
      */
     public function index(Request $request, EvaValueEntity $entity): Response
     {
         $entityId = $request->get('entity_id/d');
+        $entityTypeId = $request->get('entity_type_id/d');
         if (!$entityId) {
             return error_response('缺少实体ID');
         }
+        if (!$entityTypeId) {
+            return error_response('缺少实体类型ID');
+        }
         // 查询该实体的所有属性值
         $list = $entity->where([
-            'entity_id' => $entityId
+            'entity_id' => $entityId,
+            'entity_type_id' => $entityTypeId,
         ])->select();
         return success_response($list);
     }
@@ -47,6 +52,7 @@ class ValueController
         foreach ($data as $item) {
             if (
                 empty($item['entity_id']) ||
+                empty($item['entity_type_id']) ||
                 empty($item['attribute_id']) ||
                 !isset($item['value'])
             ) {
@@ -59,6 +65,7 @@ class ValueController
             }
             $saveData = [
                 'entity_id'    => $item['entity_id'],
+                'entity_type_id' => $item['entity_type_id'],
                 'attribute_id' => $item['attribute_id'],
             ];
             switch ($attr->backend_type) {
@@ -78,6 +85,7 @@ class ValueController
             // 先查找是否已存在,存在则更新,否则新增
             $exist = $entity->where([
                 'entity_id' => $item['entity_id'],
+                'entity_type_id' => $item['entity_type_id'],
                 'attribute_id' => $item['attribute_id']
             ])->find();
             if ($exist) {
@@ -91,15 +99,16 @@ class ValueController
 
     /**
      * 删除指定实体的所有属性值
-     * DELETE /eav/value?entity_id=123
+     * DELETE /eav/value?entity_id=123&entity_type_id=1
      */
     public function delete(Request $request, EvaValueEntity $entity): Response
     {
         $entityId = $request->get('entity_id/d');
-        if (!$entityId) {
+        $entityTypeId = $request->get('entity_type_id/d');
+        if (!$entityId || !$entityTypeId) {
             return error_response('参数缺失');
         }
-        $entity->where(['entity_id' => $entityId])->delete();
+        $entity->where(['entity_id' => $entityId, 'entity_type_id' => $entityTypeId])->delete();
         return success_response('删除成功');
     }
-}
+}