From 71e1e6d08f5529e2c6e0418a8b4193fe73a21328 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Sat, 15 Feb 2025 18:45:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=20mUpdate=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=89=B9=E9=87=8F=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../think-library/src/helper/QueryHelper.php | 2 +- .../src/service/SystemService.php | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/plugin/think-library/src/helper/QueryHelper.php b/plugin/think-library/src/helper/QueryHelper.php index ab5330d41..46fa860e4 100644 --- a/plugin/think-library/src/helper/QueryHelper.php +++ b/plugin/think-library/src/helper/QueryHelper.php @@ -386,7 +386,7 @@ class QueryHelper extends Helper 'mSave' => [SaveHelper::class, 'init'], 'mQuery' => [QueryHelper::class, 'init'], 'mDelete' => [DeleteHelper::class, 'init'], - 'mUpdate' => [SystemService::class, 'save'], + 'mUpdate' => [SystemService::class, 'batchUpdate'], ]; if (isset($hooks[$method])) { [$class, $method] = $hooks[$method]; diff --git a/plugin/think-library/src/service/SystemService.php b/plugin/think-library/src/service/SystemService.php index ed0d9f6ee..095860aea 100644 --- a/plugin/think-library/src/service/SystemService.php +++ b/plugin/think-library/src/service/SystemService.php @@ -177,6 +177,26 @@ class SystemService extends Service } } + /** + * 批量更新保存数据 + * @param Model|Query|string $query 数据查询对象 + * @param array $data 需要保存的数据,成功返回对应模型 + * @param string $key 更新条件查询主键 + * @param mixed $map 额外更新查询条件 + * @return boolean|integer 失败返回 false, 成功返回主键值或 true + * @throws \think\admin\Exception + */ + public static function batchUpdate($query, array &$data, string $key = 'id', $map = []) + { + try { + $query = Helper::buildQuery($query)->master()->strict(false); + if (empty($map[$key])) $query->where([$key => $data[$key] ?? null]); + return $query->where($map)->update($data); + } catch (\Exception $exception) { + throw new Exception($exception->getMessage(), $exception->getCode()); + } + } + /** * 解析缓存名称 * @param string $rule 配置名称