Skip to content

Commit 548e80e

Browse files
committed
fix: 修改 mUpdate 支持批量更新
1 parent c776b05 commit 548e80e

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/helper/QueryHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ public static function make($model, string $method = 'init', array $args = [], ?
386386
'mSave' => [SaveHelper::class, 'init'],
387387
'mQuery' => [QueryHelper::class, 'init'],
388388
'mDelete' => [DeleteHelper::class, 'init'],
389-
'mUpdate' => [SystemService::class, 'save'],
389+
'mUpdate' => [SystemService::class, 'batchUpdate'],
390390
];
391391
if (isset($hooks[$method])) {
392392
[$class, $method] = $hooks[$method];

src/service/SystemService.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,26 @@ public static function save($query, array &$data, string $key = 'id', $map = [])
177177
}
178178
}
179179

180+
/**
181+
* 批量更新保存数据
182+
* @param Model|Query|string $query 数据查询对象
183+
* @param array $data 需要保存的数据,成功返回对应模型
184+
* @param string $key 更新条件查询主键
185+
* @param mixed $map 额外更新查询条件
186+
* @return boolean|integer 失败返回 false, 成功返回主键值或 true
187+
* @throws \think\admin\Exception
188+
*/
189+
public static function batchUpdate($query, array &$data, string $key = 'id', $map = [])
190+
{
191+
try {
192+
$query = Helper::buildQuery($query)->master()->strict(false);
193+
if (empty($map[$key])) $query->where([$key => $data[$key] ?? null]);
194+
return $query->where($map)->update($data);
195+
} catch (\Exception $exception) {
196+
throw new Exception($exception->getMessage(), $exception->getCode());
197+
}
198+
}
199+
180200
/**
181201
* 解析缓存名称
182202
* @param string $rule 配置名称

0 commit comments

Comments
 (0)