Skip to content

Commit f3cc89a

Browse files
committed
视图模型增加reflesh方法 原allowWrite参数改为readonly参数 默认支持写入 除非设置readonly为true
1 parent 6c560ac commit f3cc89a

File tree

3 files changed

+84
-58
lines changed

3 files changed

+84
-58
lines changed

src/Model.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,7 @@ public static function update(array | object $data, $where = [], array $allowFie
652652

653653
$model->allowField($allowField)->exists(true)->save($data, $where);
654654
// 刷新数据
655-
$model->refresh();
656-
657-
return $model->fetchModel($model);
655+
return $model->fetchModel($model->refresh());
658656
}
659657

660658
/**

src/model/View.php

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public function __construct(?Model $model = null, bool $with = false)
3737
{
3838
parent::__construct($model);
3939

40-
if (!$this->getOption('allowWrite', false)) {
41-
// 设置为视图模型
40+
if ($this->getOption('readonly')) {
41+
// 设置为只读视图模型
4242
$this->model()->asView(true);
4343
}
4444

@@ -193,6 +193,19 @@ public function data(array $data)
193193
return $this;
194194
}
195195

196+
/**
197+
* 刷新模型数据.
198+
*
199+
* @return $this
200+
*/
201+
public function refresh()
202+
{
203+
if ($this->isEmpty()) {
204+
$this->initData();
205+
}
206+
return $this;
207+
}
208+
196209
/**
197210
* 清空视图模型数据
198211
*
@@ -348,9 +361,11 @@ protected function convertData(): array
348361
$last = array_pop($fields);
349362
$target = $this->model();
350363
foreach ($fields as $attr) {
351-
$target = $target->$attr;
364+
$target = $target?->$attr;
365+
}
366+
if ($target) {
367+
$target->$last = $data[$key];
352368
}
353-
$target->$last = $data[$key];
354369
} elseif (is_int($key) && isset($mapping[$field])) {
355370
[$relation] = explode('->', $mapping[$field]);
356371
$together[] = $relation;
@@ -396,7 +411,7 @@ protected function validate(array $data = [], array $allow = []): array
396411
*/
397412
public function save(): bool
398413
{
399-
if (!$this->getOption('allowWrite', false)) {
414+
if ($this->getOption('readonly')) {
400415
return false;
401416
}
402417

@@ -410,6 +425,53 @@ public function save(): bool
410425
return $this->model()->save($data);
411426
}
412427

428+
/**
429+
* 删除模型数据.
430+
*
431+
* @return bool
432+
*/
433+
public function delete(): bool
434+
{
435+
if ($this->model()->delete()) {
436+
$this->clear();
437+
return true;
438+
}
439+
440+
return false;
441+
}
442+
443+
/**
444+
* 写入数据.
445+
*
446+
* @param array|object $data 数据
447+
* @return static
448+
*/
449+
public static function create(array | object $data)
450+
{
451+
$entity = new static();
452+
$model = $entity->model()->exists(false)->save($data, true);
453+
$entity->refresh();
454+
455+
return $entity;
456+
}
457+
458+
/**
459+
* 更新数据.
460+
*
461+
* @param array|object $data 数据
462+
* @param mixed $where 更新条件
463+
* @param array $allowField 允许字段
464+
* @return static
465+
*/
466+
public static function update(array | object $data, $where = [], array $allowField = [])
467+
{
468+
$entity = new static();
469+
$model = $entity->model()->allowField($allowField)->exists(true)->save($data, $where);
470+
$entity->refresh();
471+
472+
return $entity;
473+
}
474+
413475
/**
414476
* 获取属性 支持获取器
415477
*
@@ -501,53 +563,6 @@ public function __unserialize(array $data)
501563
}
502564
}
503565

504-
/**
505-
* 删除模型数据.
506-
*
507-
* @return bool
508-
*/
509-
public function delete(): bool
510-
{
511-
if ($this->model()->delete()) {
512-
$this->clear();
513-
return true;
514-
}
515-
516-
return false;
517-
}
518-
519-
/**
520-
* 写入数据.
521-
*
522-
* @param array|object $data 数据
523-
* @return static
524-
*/
525-
public static function create(array | object $data)
526-
{
527-
$entity = new static();
528-
$model = $entity->model()->exists(false)->create($data);
529-
$entity->setModel($model)->initData();
530-
531-
return $entity;
532-
}
533-
534-
/**
535-
* 更新数据.
536-
*
537-
* @param array|object $data 数据
538-
* @param mixed $where 更新条件
539-
* @param array $allowField 允许字段
540-
* @return static
541-
*/
542-
public static function update(array | object $data, $where = [], array $allowField = [])
543-
{
544-
$entity = new static();
545-
$model = $entity->model()->update($data, $where, $allowField);
546-
$entity->setModel($model)->initData();
547-
548-
return $entity;
549-
}
550-
551566
public static function __callStatic($method, $args)
552567
{
553568
$entity = new static();

tests/orm/ModelViewTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class UserViewModel extends View
2424
protected function getOptions(): array
2525
{
2626
return [
27-
'modelClass' => TestUserModel::class,
27+
'modelClass' => TestUserModel::class,
28+
'autoMapping' => ['profile'],
2829
'viewMapping' => [
2930
'nickname' => 'name',
3031
'user_age' => 'age',
@@ -33,6 +34,11 @@ protected function getOptions(): array
3334
],
3435
];
3536
}
37+
38+
public function setReadonly(bool $readonly)
39+
{
40+
return $this->setOption('readonly', $readonly);
41+
}
3642
}
3743

3844
class TestUserModel extends Model
@@ -141,10 +147,17 @@ public function testViewModelRelation()
141147
$this->assertFalse(isset($viewModel->not_exist));
142148

143149
// 测试视图模型写入限制
150+
$viewModel->setReadonly(true); // 只读模型
151+
$viewModel->nickname = 'new_name';
152+
$viewModel->save();
153+
$viewModel = UserViewModel::find($user->id);
154+
$this->assertEquals('test2', $viewModel->nickname);
155+
156+
$viewModel->setReadonly(false); // 可写模型
144157
$viewModel->nickname = 'new_name';
145158
$viewModel->save();
146159
$viewModel = UserViewModel::find($user->id);
147-
$this->assertEquals('test2', $viewModel->nickname); // 视图模型不允许修改数据
160+
$this->assertEquals('new_name', $viewModel->nickname);
148161
}
149162

150163
public function testEmptyViewModel()
@@ -178,7 +191,7 @@ public function testViewModelWithRelation()
178191
]);
179192

180193
// 加载关联数据
181-
$viewModel = UserViewModel::with(['profile'])->find($user->id);
194+
$viewModel = UserViewModel::find($user->id);
182195

183196
$this->assertEquals('test3', $viewModel->nickname);
184197
$this->assertEquals('test_test3', $viewModel->test_name);

0 commit comments

Comments
 (0)