Skip to content

Commit 1171a6e

Browse files
committed
改进视图模型的关联查询
1 parent ea08c17 commit 1171a6e

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/model/View.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,31 @@ public function __construct(?Model $model = null, bool $with = false)
3939
$this->model()->asView(true);
4040

4141
// 初始化模型
42-
if (!$this->isEmpty() && !$with) {
43-
$this->initData();
42+
if (!$this->isEmpty()) {
43+
$this->initData($with);
4444
}
4545
}
4646

4747
/**
4848
* 初始化实体数据属性(如果存在关联查询则会延迟执行).
4949
*
50+
* @param bool $with 是否处理关联数据
5051
* @return void
5152
*/
52-
public function initData()
53+
public function initData(bool $with = true)
5354
{
5455
// 获取实体属性
5556
$properties = $this->getEntityProperties();
5657
$data = $this->model()->getData();
5758
foreach ($properties as $key => $field) {
59+
if (!$with) {
60+
// 确保存在基础模型数据
61+
if (isset($data[$field])) {
62+
$this->$field = $data[$field];
63+
}
64+
continue;
65+
}
66+
5867
if (is_int($key)) {
5968
$this->$field = $this->fetchViewAttr($field);
6069
} elseif (strpos($field, '->')) {

0 commit comments

Comments
 (0)