This commit is contained in:
Aether
2025-09-25 09:12:22 +08:00
parent f286e18e71
commit 25aa56439a
13 changed files with 171 additions and 167 deletions

View File

@@ -12,8 +12,8 @@ use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Collection;
use Hyperf\Database\Model\ModelNotFoundException;
use Hyperf\DbConnection\Model\Model as HyperfModel;
use Hyperf\DbConnection\Db;
use Hyperf\DbConnection\Model\Model as HyperfModel;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\ModelCache\Cacheable;
use Hyperf\ModelCache\CacheableInterface;
@@ -60,13 +60,19 @@ abstract class AetherModel extends HyperfModel implements CacheableInterface
* 排序配置:
* - false: 禁用排序
* - 字符串: 排序字段(默认升序)
* - 数组: ['field' => '字段名', 'direction' => 'asc/desc']
* - 数组: ['field' => '字段名', 'direction' => 'asc/desc'].
*/
protected string|array|bool $sortable = 'sort'; // 默认按sort字段升序
protected array|bool|string $sortable = 'sort'; // 默认按sort字段升序
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->bootBaseModel();
}
/**
* 获取排序配置
* @return array|null [field, direction] 或 null禁用排序
* 获取排序配置.
* @return null|array [field, direction] 或 null禁用排序
*/
public function getSortConfig(): ?array
{
@@ -78,19 +84,13 @@ abstract class AetherModel extends HyperfModel implements CacheableInterface
if (is_string($this->sortable)) {
return [
'field' => $this->sortable,
'direction' => 'asc'
'direction' => 'asc',
];
}
return null;
}
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->bootBaseModel();
}
/**
* 快捷创建.
*/
@@ -108,7 +108,7 @@ abstract class AetherModel extends HyperfModel implements CacheableInterface
}
/**
* 快捷删除指定ID的记录
* 快捷删除指定ID的记录.
*
* @param int $id 要删除的记录ID
* @return bool 成功删除返回true
@@ -117,19 +117,18 @@ abstract class AetherModel extends HyperfModel implements CacheableInterface
*/
public static function deleteById(int $id): bool
{
if (!static::query()->where('id', $id)->exists()) {
if (! static::query()->where('id', $id)->exists()) {
throw new ModelNotFoundException(sprintf(
'找不到ID为 %d 的 %s 记录',
$id,
static::class
));
'找不到ID为 %d 的 %s 记录',
$id,
static::class
));
}
return static::query()->where('id', $id)->delete() > 0;
}
/**
* 快捷查找.
* @param int $id
* @return Builder|Builder[]|Collection|HyperfModel
* @throws Exception 当删除操作发生其他错误时抛出
* @throws ModelNotFoundException
@@ -147,21 +146,21 @@ abstract class AetherModel extends HyperfModel implements CacheableInterface
* 快捷查找或失败.
* @param int $id 要查找的记录ID
* @return AetherModel 根据ID查找记录不存在则抛出异常
* 根据ID查找记录不存在则抛出异常
* 根据ID查找记录不存在则抛出异常
* @throws Exception 当删除操作发生其他错误时抛出
* @throws ModelNotFoundException 当记录不存在时抛出
*/
public static function findOrFailById(int $id): static
{
$record = static::query()->find($id);
if(is_null($record)) {
if (is_null($record)) {
throw new ModelNotFoundException(sprintf(
'找不到ID为 %d 的 %s 记录',
$id,
static::class
));
}
return $record;// static::query()->findOrFail($id);
return $record; // static::query()->findOrFail($id);
}
/**
@@ -406,7 +405,7 @@ abstract class AetherModel extends HyperfModel implements CacheableInterface
{
foreach ($conditions as $field => $value) {
// 跳过非字符串的字段名(防止索引数组键导致的类型错误)
if (!is_string($field)) {
if (! is_string($field)) {
continue;
}