From 61d131b99fb0df1a97ce37051b011fe266a49154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8A=E5=A4=A9?= <442384644@qq.com> Date: Fri, 23 Oct 2026 14:09:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=95=86=E5=93=81):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=94=B6=E8=97=8F/=E7=82=B9=E8=B5=9E=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E5=86=99=E5=85=A5=E8=A2=AB=20saveAll=20=E9=9D=99=E9=BB=98?= =?UTF-8?q?=E4=B8=A2=E5=BC=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - eb_store_product_relation 使用 (uid, product_id, type, category) 联合主键, Model::saveAll() 因主键字段齐全走 UPDATE 分支,导致新记录静默写入失败; DAO 新增 insertAll() 直连模型 insertAll 绕过该语义。 - 写入前过滤非正数商品ID(前端未取到 product_id 时会传空值),避免脏数据。 - 批量写入统一使用同一 add_time,便于按批次检索。 --- .../product/product/StoreProductRelationDao.php | 12 ++++++++++++ .../product/StoreProductRelationServices.php | 14 +++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/backend/app/dao/product/product/StoreProductRelationDao.php b/backend/app/dao/product/product/StoreProductRelationDao.php index 4b9a38f6a..9d2fb1e6c 100644 --- a/backend/app/dao/product/product/StoreProductRelationDao.php +++ b/backend/app/dao/product/product/StoreProductRelationDao.php @@ -29,6 +29,18 @@ class StoreProductRelationDao extends BaseDao return StoreProductRelation::class; } + /** + * 批量插入,绕过模型 saveAll 语义 + * 该表使用 (uid, product_id, type, category) 联合主键,saveAll() 会因主键字段齐全 + * 而按 UPDATE 处理,导致新记录静默写入失败 + * @param array $data + * @return int + */ + public function insertAll(array $data): int + { + return $this->getModel()->insertAll($data); + } + /** * 获取收藏列表 * @param array $where diff --git a/backend/app/services/product/product/StoreProductRelationServices.php b/backend/app/services/product/product/StoreProductRelationServices.php index e41cffce6..b7f1f6cc6 100644 --- a/backend/app/services/product/product/StoreProductRelationServices.php +++ b/backend/app/services/product/product/StoreProductRelationServices.php @@ -170,15 +170,23 @@ class StoreProductRelationServices extends BaseService $relationType = strtolower($relationType); $category = strtolower($category); $relationData = []; - $productIdS = array_unique($productIdS); + // 过滤非法商品ID(如前端未取到 product_id 时传入的空值),避免写入 product_id=0 的脏数据 + $productIdS = array_values(array_unique(array_filter(array_map('intval', $productIdS), function ($productId) { + return $productId > 0; + }))); + if (!$productIdS) { + return true; + } $relationProductIdS = $this->dao->getColumn(['uid' => $uid, 'type' => $relationType, 'category' => $category, 'product_id' => $productIdS], 'product_id'); + $addTime = time(); foreach ($productIdS as $productId) { if (!in_array($productId, $relationProductIdS)) { - $relationData[] = ['uid' => $uid, 'product_id' => $productId, 'type' => $relationType, 'category' => $category]; + $relationData[] = ['uid' => $uid, 'product_id' => $productId, 'type' => $relationType, 'category' => $category, 'add_time' => $addTime]; } } if ($relationData) { - if (!$this->dao->saveAll($relationData)) { + // 该表为联合主键,Model::saveAll() 会因主键字段齐全而走 UPDATE 分支导致静默不写入,必须直接 insertAll + if ($this->dao->insertAll($relationData) < 1) { throw new ApiException('添加失败'); } }