Skip to content

Commit 3a3d2ce

Browse files
committed
fix: 优化 SystemService::update ,减少检测时的查询次数
1 parent c0c64a4 commit 3a3d2ce

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/service/SystemService.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,11 @@ public static function save($query, array &$data, string $key = 'id', $map = [])
189189
public static function update($query, array $data, string $key = 'id', $map = [])
190190
{
191191
try {
192-
$query = Helper::buildQuery($query)->master()->strict(false)->where($map);
192+
$query = Helper::buildQuery($query)->master()->where($map);
193193
if (empty($map[$key])) $query->where([$key => $data[$key] ?? null]);
194-
return (clone $query)->count() > 0 ? (clone $query)->update($data) : (clone $query)->findOrEmpty()->save($data);
195-
} catch (\Exception $exception) {
194+
$model = (clone $query)->findOrEmpty();
195+
return $model->isExists() ? (clone $query)->strict(false)->update($data) : $model->save($data);
196+
} catch (\Exception|\Throwable $exception) {
196197
throw new Exception($exception->getMessage(), $exception->getCode());
197198
}
198199
}

0 commit comments

Comments
 (0)