Quellcode durchsuchen

feat(eav): 更新EAV值控制器以支持批量属性值保存

- 修改数据格式为 { entity_id, entity_type_id, values: [{ attribute_id, value }, ...] }
- 添加数据格式验证逻辑
- 重构批量写入逻辑以支持单个实体的多个属性
- 添加布尔类型和日期时间类型的值处理
- 修复实体ID和实体类型ID的引用错误
- 改进错误响应消息和参数验证
runphp vor 1 Tag
Ursprung
Commit
8f6e9f1858
1 geänderte Dateien mit 27 neuen und 10 gelöschten Zeilen
  1. 27 10
      src/Controller/ValueController.php

+ 27 - 10
src/Controller/ValueController.php

@@ -41,20 +41,31 @@ class ValueController
      * 保存/更新指定实体的属性值
      * POST /eav/value
      * body: [
-     *   { "entity_id": 123, "entity_type_id": 1, "attribute_id": 2, "value": "红色" }
+     *   { "entity_id": 123, "entity_type_id": 1,  "value": [{"attribute_id": 2, value:"红色"}] }
      * ]
      */
     public function save(Request $request, EavValueEntity $entity): Response
     {
         $data = $request->post();
-        if (empty($data) || !is_array($data)) {
+        if (empty($data)) {
             return error_response('参数错误');
         }
-        // 支持批量写入
-        foreach ($data as $item) {
+        
+        // 期望的数据格式:{ entity_id, entity_type_id, values: [{ attribute_id, value }, ...] }
+        if (!isset($data['values']) || !is_array($data['values'])) {
+            return error_response('数据格式错误,需要 values 数组');
+        }
+        
+        $entityId = $data['entity_id'] ?? 0;
+        $entityTypeId = $data['entity_type_id'] ?? 0;
+        
+        if (!$entityId || !$entityTypeId) {
+            return error_response('缺少实体 ID 或实体类型 ID');
+        }
+        
+        // 批量写入
+        foreach ($data['values'] as $item) {
             if (
-                empty($item['entity_id']) ||
-                empty($item['entity_type_id']) ||
                 empty($item['attribute_id']) ||
                 !isset($item['value'])
             ) {
@@ -66,8 +77,8 @@ class ValueController
                 continue;
             }
             $saveData = [
-                'entity_id'    => $item['entity_id'],
-                'entity_type_id' => $item['entity_type_id'],
+                'entity_id'    => $entityId,
+                'entity_type_id' => $entityTypeId,
                 'attribute_id' => $item['attribute_id'],
             ];
             switch ($attr->backend_type) {
@@ -77,17 +88,23 @@ class ValueController
                 case 'decimal':
                     $saveData['value_decimal'] = floatval($item['value']);
                     break;
+                case 'boolean':
+                    $saveData['value_int'] = intval($item['value']);
+                    break;
                 case 'text':
                     $saveData['value_text'] = $item['value'];
                     break;
+                case 'datetime':
+                    $saveData['value_datetime'] = $item['value'];
+                    break;
                 case 'varchar':
                 default:
                     $saveData['value_varchar'] = $item['value'];
             }
             // 先查找是否已存在,存在则更新,否则新增
             $exist = $entity->where([
-                'entity_id' => $item['entity_id'],
-                'entity_type_id' => $item['entity_type_id'],
+                'entity_id' => $entityId,
+                'entity_type_id' => $entityTypeId,
                 'attribute_id' => $item['attribute_id']
             ])->find();
             if ($exist) {