Skip to content

Commit

Permalink
fix: 修改 mUpdate 支持批量更新
Browse files Browse the repository at this point in the history
  • Loading branch information
zoujingli committed Feb 15, 2025
1 parent c776b05 commit 548e80e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/helper/QueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ public static function make($model, string $method = 'init', array $args = [], ?
'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];
Expand Down
20 changes: 20 additions & 0 deletions src/service/SystemService.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ public static function save($query, array &$data, string $key = 'id', $map = [])
}
}

/**
* 批量更新保存数据
* @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 配置名称
Expand Down

0 comments on commit 548e80e

Please sign in to comment.