Skip to content

Commit ce62956

Browse files
committed
Db查询结果支持字段映射
1 parent e928a8e commit ce62956

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/db/concern/ModelRelationQuery.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,11 +718,15 @@ protected function resultToModel(array &$result): void
718718
$result->refreshOrigin();
719719
}
720720

721-
foreach (['hidden', 'visible', 'append', 'mapping'] as $name) {
722-
if (isset($this->options[$name])) {
721+
foreach (['hidden', 'visible', 'append'] as $name) {
722+
if (!empty($this->options[$name])) {
723723
[$value, $merge] = $this->options[$name];
724724
$result->$name($value, $merge);
725725
}
726726
}
727+
728+
if (!empty($this->options['mapping'])) {
729+
$result->mapping($this->options['mapping']);
730+
}
727731
}
728732
}

src/db/concern/ResultOperation.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ protected function result(array &$result): void
105105
if (!empty($this->options['with_attr'])) {
106106
$this->getResultAttr($result, $this->options['with_attr']);
107107
}
108+
109+
// 检查字段映射
110+
if (!empty($this->options['mapping'])) {
111+
foreach ($this->options['mapping'] as $name => $alias) {
112+
if (isset($result[$name])) {
113+
$result[$alias] = $result[$name];
114+
unset($result[$name]);
115+
}
116+
}
117+
}
108118
}
109119

110120
/**

0 commit comments

Comments
 (0)