Skip to content

Commit

Permalink
resultToModel方法取消json数据处理 模型已内置自动类型转换处理
Browse files Browse the repository at this point in the history
增加withEnumRead方法
  • Loading branch information
liu21st committed Feb 24, 2025
1 parent 5493dd2 commit 451e548
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 37 deletions.
35 changes: 0 additions & 35 deletions src/db/concern/ModelRelationQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,36 +572,6 @@ public function hasWhere(string $relation, $where = [], string $fields = '*', st
return $this->model->hasWhere($relation, $where, $fields, $joinType, $this);
}

/**
* JSON字段数据转换.
*
* @param array $result 查询数据
*
* @return void
*/
protected function jsonModelResult(array &$result) : void
{
$withAttr = $this->options['with_attr'];
foreach ($this->options['json'] as $name) {
if (!isset($result[$name])) {
continue;
}

$jsonData = json_decode($result[$name], true);
if (json_last_error() !== JSON_ERROR_NONE) {
continue;
}

if (isset($withAttr[$name])) {
foreach ($withAttr[$name] as $key => $closure) {
$jsonData[$key] = $closure($jsonData[$key] ?? null, $jsonData);
}
}

$result[$name] = !$this->options['json_assoc'] ? (object) $jsonData : $jsonData;
}
}

/**
* 查询数据转换为模型数据集对象
*
Expand Down Expand Up @@ -648,11 +618,6 @@ protected function resultSetToModelCollection(array $resultSet): ModelCollection
*/
protected function resultToModel(array &$result): void
{
// JSON数据处理
if (!empty($this->options['json'])) {
$this->jsonModelResult($result);
}

// 实时读取延迟数据
if (!empty($this->options['lazy_fields'])) {
$id = $this->getKey($result);
Expand Down
24 changes: 22 additions & 2 deletions src/model/concern/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ protected function readTransform($value, string | array $type)
$value = $type::get($value, $model);
} elseif (is_subclass_of($type, BackedEnum::class)) {
$value = $type::from($value);
if (is_subclass_of($type, EnumTransform::class)) {
$value = $value->value();
} elseif ($model->getOption('enumReadName')) {
$method = $model->getOption('enumReadName');
$value = is_string($method) ? $value->$method() : $value->name;
}
} else {
// 对象类型
$value = new $type($value);
Expand All @@ -222,7 +228,7 @@ protected function readTransform($value, string | array $type)
};

return match ($type) {
'string' => (string) $value,
'string' => (string) $value,
'int' => (int) $value,
'float' => empty($param) ? (float) $value : (float) number_format($value, (int) $param, '.', ''),
'bool' => (bool) $value,
Expand Down Expand Up @@ -614,5 +620,19 @@ public function exists(bool $exists = true)
public function isExists(): bool
{
return $this->getOption('exists', false);
}
}

/**
* 设置枚举类型自动读取数据方式
* true 表示使用name值返回
* 字符串 表示使用枚举类的方法返回
*
* @return $this
*/
public function withEnumRead(bool | string $method = true)
{
$this->setOption('enumReadName', $method);

return $this;
}
}

0 comments on commit 451e548

Please sign in to comment.