fix: 修改 mUpdate 支持批量更新

This commit is contained in:
邹景立 2025-02-15 18:45:05 +08:00
parent f3bc37e772
commit 71e1e6d08f
2 changed files with 21 additions and 1 deletions

View File

@ -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];

View File

@ -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 配置名称