diff --git a/src/Service.php b/src/Service.php index 049856a..c5ffda4 100644 --- a/src/Service.php +++ b/src/Service.php @@ -26,6 +26,7 @@ use Hyperf\DbConnection\Db; */ abstract class Service { + protected ?Validator $validator = null; protected array $search = []; protected array $ignoreSearchFields = []; @@ -50,7 +51,7 @@ abstract class Service */ public function create(array $data): mixed { - $this->validator->scene('create', $data)->check(); + $this->validator?->scene('create', $data)->check(); return $this->repository->create($data); } @@ -59,7 +60,7 @@ abstract class Service */ public function save(array $data): mixed { - $this->validator->scene('create', $data)->check(); + $this->validator?->scene('create', $data)->check(); return $this->create($data); } @@ -69,7 +70,7 @@ abstract class Service public function updateById(mixed $id, array $data): mixed { // 数据验证 - $this->validator->scene('update', $data)->check(); + $this->validator?->scene('update', $data)->check(); return $this->repository->updateById($id, $data); } @@ -153,7 +154,7 @@ abstract class Service $this->checkResourceExists($resource); // 数据验证 - $this->validator->scene('update', $data)->check(); + $this->validator?->scene('update', $data)->check(); // 钩子:处理更新时的特殊逻辑(如禁止自身为父级) $this->handleUpdateSpecialLogic($id, $data);