Skip to content

Commit 463565d

Browse files
committed
改进视图模型的refresh方法调用
1 parent f67934d commit 463565d

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

src/model/View.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(?Model $model = null)
3636
{
3737
parent::__construct($model);
3838

39-
// 初始化模型
39+
// 初始化模型数据
4040
$this->initData();
4141
}
4242

@@ -45,7 +45,7 @@ public function __construct(?Model $model = null)
4545
*
4646
* @return void
4747
*/
48-
public function initData()
48+
protected function initData()
4949
{
5050
if ($this->isEmpty()) {
5151
return ;
@@ -108,7 +108,6 @@ private function fetchViewAttr(string $field)
108108
}
109109
}
110110
}
111-
112111
return $value;
113112
}
114113

@@ -146,7 +145,6 @@ private function getEntityPropertiesMap(): array
146145
$properties[] = $field;
147146
}
148147
}
149-
150148
$this->setOption('view_properties', $properties);
151149
}
152150

@@ -192,9 +190,7 @@ public function data(array $data)
192190
*/
193191
public function refresh()
194192
{
195-
if ($this->isEmpty()) {
196-
$this->initData();
197-
}
193+
$this->initData();
198194
return $this;
199195
}
200196

@@ -450,9 +446,8 @@ public static function create(array | object $data)
450446
{
451447
$entity = new static();
452448
$model = $entity->model()->exists(false)->save($data, true);
453-
$entity->refresh();
454-
455-
return $entity;
449+
// 刷新视图模型数据
450+
return $entity->refresh();
456451
}
457452

458453
/**
@@ -468,9 +463,7 @@ public static function update(array | object $data, $where = [], array $allowFie
468463
$entity = new static();
469464
$model = $entity->model()->allowField($allowField)->exists(true)->save($data, $where, true);
470465
// 刷新视图模型数据
471-
$entity->refresh();
472-
473-
return $entity;
466+
return $entity->refresh();
474467
}
475468

476469
/**
@@ -571,7 +564,7 @@ public static function __callStatic($method, $args)
571564
// 调用model的静态方法
572565
$db = $entity->model();
573566
} else {
574-
// 调用Query类查询方法
567+
// 处理字段映射
575568
$map = $entity->getOption('viewMapping', []);
576569
$db = $entity->model()->db()->fieldMap($map);
577570
}

src/model/concern/RelationShip.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,10 @@ public function eagerlyResultSet(array $resultSet, array $relations, array $with
448448
$relationResult->eagerlyResultSet($resultSet, $relationName, $subRelation, $closure, $relationCache, $join);
449449
}
450450

451-
// 视图模型初始化数据
451+
// 刷新视图模型数据
452452
foreach ($resultSet as $result) {
453453
if ($result instanceof View) {
454-
$result->initData();
454+
$result->refresh();
455455
}
456456
}
457457
}
@@ -504,8 +504,8 @@ public function eagerlyResult(Model $result, array $relations, array $withRelati
504504
}
505505

506506
if ($result instanceof View) {
507-
// 视图模型初始化数据
508-
$result->initData();
507+
// 刷新视图模型数据
508+
$result->refresh();
509509
}
510510
}
511511

0 commit comments

Comments
 (0)