核心库补齐Aether兼容方法与树形Trait,中间件复用RPC上下文
This commit is contained in:
100
src/Model.php
100
src/Model.php
@@ -119,13 +119,7 @@ abstract class Model extends HyperfModel implements CacheableInterface
|
||||
}
|
||||
|
||||
$this->applySearch($query, $params);
|
||||
|
||||
if ($this->sortable) {
|
||||
$sortConfig = $this->getSortConfig();
|
||||
if ($sortConfig) {
|
||||
$query->orderBy($sortConfig['field'], $sortConfig['direction']);
|
||||
}
|
||||
}
|
||||
$this->applyOrdering($query, $params);
|
||||
|
||||
$withDeleted = filter_var($params['with_deleted'] ?? false, FILTER_VALIDATE_BOOLEAN);
|
||||
if ($withDeleted) {
|
||||
@@ -208,12 +202,48 @@ abstract class Model extends HyperfModel implements CacheableInterface
|
||||
$searchParams = array_intersect_key($params, array_flip($allowedSearchFields));
|
||||
|
||||
// 合并系统参数与合法搜索参数(排除其他未知参数)
|
||||
$systemParams = ['page', 'size', 'with_deleted', 'parent_id'];
|
||||
$systemParams = ['page', 'size', 'with_deleted', 'parent_id', 'order_by', 'order_direction'];
|
||||
$validSystemParams = array_intersect_key($params, array_flip($systemParams));
|
||||
|
||||
return array_merge($validSystemParams, $searchParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用排序逻辑,支持自定义 order_by/order_direction.
|
||||
*/
|
||||
protected function applyOrdering(Builder $query, array $params): void
|
||||
{
|
||||
$orderField = $params['order_by'] ?? null;
|
||||
$orderDirection = strtolower((string) ($params['order_direction'] ?? 'asc'));
|
||||
|
||||
if (is_string($orderField) && $this->isValidOrderField($orderField)) {
|
||||
$direction = $orderDirection === 'desc' ? 'desc' : 'asc';
|
||||
$query->orderBy($orderField, $direction);
|
||||
if ($orderField !== 'id' && ! str_contains($orderField, '.')) {
|
||||
$query->orderBy('id', 'asc');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->sortable) {
|
||||
$sortConfig = $this->getSortConfig();
|
||||
if ($sortConfig) {
|
||||
$query->orderBy($sortConfig['field'], $sortConfig['direction']);
|
||||
if ($sortConfig['field'] !== 'id' && ! str_contains($sortConfig['field'], '.')) {
|
||||
$query->orderBy('id', 'asc');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验排序字段,避免 SQL 注入.
|
||||
*/
|
||||
protected function isValidOrderField(string $field): bool
|
||||
{
|
||||
return (bool) preg_match('/^[a-zA-Z0-9_\.]+$/', $field);
|
||||
}
|
||||
|
||||
/**
|
||||
* 快捷创建.
|
||||
*/
|
||||
@@ -290,6 +320,60 @@ abstract class Model extends HyperfModel implements CacheableInterface
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找记录,不存在时抛出业务异常.
|
||||
*/
|
||||
public static function findOrThrow($id, string $message = '数据不存在'): static
|
||||
{
|
||||
$record = static::query()->find($id);
|
||||
if (is_null($record)) {
|
||||
throw new BusinessException($message, 404);
|
||||
}
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aether 兼容:创建记录并返回 ID.
|
||||
*/
|
||||
public static function createBy(array $data): int
|
||||
{
|
||||
return static::query()->create($data)->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aether 兼容:按主键查找,不存在时抛出业务异常.
|
||||
*/
|
||||
public static function findByPk(int $id): array|Builder|Collection|HyperfModel
|
||||
{
|
||||
$record = static::query()->find($id);
|
||||
if (is_null($record)) {
|
||||
throw new BusinessException(sprintf('找不到 ID 为 %d 的记录', $id), 404);
|
||||
}
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aether 兼容:按主键更新.
|
||||
*/
|
||||
public static function updateByPk(int $id, array $data): bool
|
||||
{
|
||||
if (! static::query()->where('id', $id)->exists()) {
|
||||
throw new BusinessException(sprintf('找不到 ID 为 %d 的记录', $id), 404);
|
||||
}
|
||||
return static::query()->where('id', $id)->update($data) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aether 兼容:按主键删除.
|
||||
*/
|
||||
public static function deleteByPk(int $id): bool
|
||||
{
|
||||
if (! static::query()->where('id', $id)->exists()) {
|
||||
throw new BusinessException(sprintf('找不到 ID 为 %d 的记录', $id), 404);
|
||||
}
|
||||
return static::query()->where('id', $id)->delete() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取器处理.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user