Skip to content

Commit

Permalink
改进指定字段别名后查询数据问题
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Mar 3, 2025
1 parent cfd13d3 commit 22286e3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/db/concern/ModelRelationQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,17 +665,16 @@ protected function resultToModel(array &$result): void
if (!empty($this->options['with_attr'])) {
$result->withFieldAttr($this->options['with_attr']);
}
// 刷新原始数据
$result->refreshOrigin();


// 模型输出设置
foreach (['hidden', 'visible', 'append'] as $name) {
if (!empty($this->options[$name])) {
[$value, $merge] = $this->options[$name];
$result->$name($value, $merge);
}
}

// 字段映射
if (!empty($this->options['mapping'])) {
$result->mapping($this->options['mapping']);
}
Expand Down
5 changes: 4 additions & 1 deletion src/model/concern/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private function initializeData(array | object $data, bool $fromSave = false)
$type = $schema[$trueName] ?? 'string';
}

if ($this->isView() || $this->isVirtual() || in_array($trueName, $fields)) {
if (in_array($trueName, $fields)) {
// 读取数据后进行类型转换
if (!$fromSave || !$this->hasSetAttr($trueName)) {
$value = $this->readTransform($value, $type);
Expand All @@ -89,6 +89,9 @@ private function initializeData(array | object $data, bool $fromSave = false)
$this->setData($trueName, $value);
// 记录原始数据
$origin[$trueName] = $value;
} else {
// 非数据表字段 额外赋值
$this->setData($trueName, $value);
}
}

Expand Down

0 comments on commit 22286e3

Please # to comment.