bianjunhui 4 天之前
父节点
当前提交
2bcf210825

+ 5 - 3
packages/Webkul/BagistoApi/src/Models/ProductReview.php

@@ -8,7 +8,6 @@ use ApiPlatform\Metadata\GraphQl\Mutation;
 use ApiPlatform\Metadata\GraphQl\Query;
 use ApiPlatform\Metadata\GraphQl\QueryCollection;
 use ApiPlatform\Metadata\Link;
-use ApiPlatform\Metadata\ApiProperty;
 use Webkul\BagistoApi\Dto\CreateProductReviewInput;
 use Webkul\BagistoApi\Dto\UpdateProductReviewInput;
 use Webkul\BagistoApi\Resolver\BaseQueryItemResolver;
@@ -443,11 +442,14 @@ class ProductReview extends \Webkul\Product\Models\ProductReview
 
     public function getAttachmentUrls()
     {
-        if ($this->images->isEmpty()) {
+        // 强制重新查询,避免 save() 后关系集合未刷新导致 attachments 为空
+        $images = $this->images()->get();
+
+        if ($images->isEmpty()) {
             return [];
         }
 
-        return $this->images->map(function ($item) {
+        return $images->map(function ($item) {
             return [
                 'type' => $item->type,
                 'url'  => $item->url(),

+ 25 - 15
packages/Webkul/BagistoApi/src/State/ProductReviewProcessor.php

@@ -48,6 +48,9 @@ class ProductReviewProcessor implements ProcessorInterface
             if (method_exists($request, 'all')) {
                 $allData = $request->all();
 
+                // images / attachments 不是主表字段,先剔除,避免 fill 时污染模型属性
+                unset($allData['images'], $allData['attachments'], $allData['Images'], $allData['Attachments']);
+
                 $data->fill($allData);
 
                 // API Platform 使用 SnakeCaseToCamelCaseNameConverter,请求体字段会被
@@ -75,7 +78,25 @@ class ProductReviewProcessor implements ProcessorInterface
         }
         $this->validateReview($data);
 
-        return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
+        $saved = $this->persistProcessor->process($data, $operation, $uriVariables, $context);
+
+        // REST 创建接口:处理图片上传(与 GraphQL handleCreate 保持一致)
+        if ($operation instanceof \ApiPlatform\Metadata\Post && $saved instanceof ProductReview) {
+            $request = $context['request'] ?? request();
+            $images = $request->input('images');
+            $attachmentsInput = $request->input('attachments');
+
+            if (! empty($images) && is_array($images)) {
+                $this->saveImagesToS3($images, $saved->id);
+            } elseif (! empty($attachmentsInput)) {
+                $this->saveAttachments(
+                    is_array($attachmentsInput) ? json_encode($attachmentsInput) : $attachmentsInput,
+                    $saved->id
+                );
+            }
+        }
+
+        return $saved;
     }
 
     /**
@@ -125,10 +146,6 @@ class ProductReviewProcessor implements ProcessorInterface
             $attachments = $this->saveAttachments($data->attachments, $review->id);
         }
 
-        if ($attachments) {
-            $review->setAttribute('attachments', json_encode($attachments));
-        }
-
         return $this->mapToOutput($review);
     }
 
@@ -170,21 +187,14 @@ class ProductReviewProcessor implements ProcessorInterface
         $review->save();
 
         // 处理图片上传(新方式:使用 images 字段)
-        $attachments = [];
-
         if (! empty($data->images) && is_array($data->images)) {
-            $attachments = $this->saveImagesToS3($data->images, $review->id);
+            $this->saveImagesToS3($data->images, $review->id);
         } elseif (! empty($data->attachments)) {
             // 向后兼容:使用旧的 attachments 字段(Base64格式)
-            $attachments = $this->saveAttachments($data->attachments, $review->id);
+            $this->saveAttachments($data->attachments, $review->id);
         }
 
-        if ($attachments) {
-            $review->setAttribute('attachments', json_encode($attachments));
-            $review->save();
-        }
-
-	    return $this->mapToOutput($review);
+        return $this->mapToOutput($review);
     }
 
     /**