再次优化

This commit is contained in:
阿不叮咚
2025-09-24 15:51:38 +08:00
parent edd4effc95
commit 998f8d8356
3 changed files with 15 additions and 10 deletions

View File

@@ -60,9 +60,9 @@ abstract class AbstractModel extends AetherModel
* @param array $columns * @param array $columns
* @return AbstractModel * @return AbstractModel
*/ */
public static function findOrFail(int $id, $columns = []): self public static function findOrFail(mixed $id, $columns = []): self
{ {
$model = self::find($id); $model = self::find($id, $columns);
if (!$model) { if (!$model) {
throw new ModelNotFoundException(); throw new ModelNotFoundException();
} }
@@ -74,10 +74,7 @@ abstract class AbstractModel extends AetherModel
*/ */
public static function createOne(array $data): self public static function createOne(array $data): self
{ {
$model = new static(); return static::query()->create($data);
$model->fill($data);
$model->save();
return $model;
} }
/** /**

View File

@@ -43,7 +43,7 @@ abstract class AetherController
/** /**
* 创建资源 (RESTFul: POST /resources) * 创建资源 (RESTFul: POST /resources)
*/ */
public function store() public function store(): array
{ {
$data = $this->request->all(); $data = $this->request->all();
$id = $this->getService()->create($data); $id = $this->getService()->create($data);
@@ -71,8 +71,8 @@ abstract class AetherController
/** /**
* 获取对应的服务类 * 获取对应的服务类
* @return \Aether\AbstractService * @return AbstractService
*/ */
abstract protected function getService(); abstract protected function getService(): AbstractService;
} }

View File

@@ -4,14 +4,21 @@ declare(strict_types=1);
namespace Aether; namespace Aether;
use Hyperf\Context\ApplicationContext;
use Hyperf\ExceptionHandler\ExceptionHandler; use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\Rpc\Protocol; use Hyperf\Rpc\Protocol;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Throwable; use Throwable;
use Hyperf\Context\Context; use Hyperf\Context\Context;
class RpcExceptionHandler extends ExceptionHandler class RpcExceptionHandler extends ExceptionHandler
{ {
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
{ {
// 微服务间调用返回更精简的错误信息 // 微服务间调用返回更精简的错误信息
@@ -21,7 +28,8 @@ class RpcExceptionHandler extends ExceptionHandler
'request_id' => Context::get('request_id', ''), 'request_id' => Context::get('request_id', ''),
]; ];
$protocol = make(Protocol::class); $protocol = ApplicationContext::getContainer()->get(Protocol::class);
// $response->getBody()->write($protocol->pack($data));
$response->getBody()->write($protocol->pack($data)); $response->getBody()->write($protocol->pack($data));
return $response; return $response;