Skip to content

Commit ad3258a

Browse files
committed
改进Model的虚拟模型判断 取消视图模型的只读功能
1 parent 00ec4b1 commit ad3258a

File tree

5 files changed

+38
-72
lines changed

5 files changed

+38
-72
lines changed

src/Model.php

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,6 @@ public function save(array | object $data = [], $where = [], bool $refresh = fal
347347
$this->initializeData($data, true);
348348
}
349349

350-
if ($this->isVirtual() || $this->isView()) {
351-
return true;
352-
}
353-
354350
if (false === $this->trigger('BeforeWrite')) {
355351
return false;
356352
}
@@ -514,7 +510,7 @@ public function isChange(string $name): bool
514510
}
515511

516512
/**
517-
* 是否为虚拟模型(不能查询).
513+
* 是否为虚拟模型(不能查询和写入).
518514
*
519515
* @return bool
520516
*/
@@ -523,26 +519,6 @@ public function isVirtual(): bool
523519
return false;
524520
}
525521

526-
/**
527-
* 设置为视图模型(不能写入).
528-
*
529-
* @return $this
530-
*/
531-
public function asView(bool $isView = true)
532-
{
533-
return $this->setOption('is_view', $isView);
534-
}
535-
536-
/**
537-
* 是否为视图模型(不能写入).
538-
*
539-
* @return bool
540-
*/
541-
public function isView(): bool
542-
{
543-
return $this->getOption('is_view', false);
544-
}
545-
546522
/**
547523
* 刷新模型数据.
548524
*
@@ -596,12 +572,6 @@ public static function saveAll(iterable $dataSet, bool $replace = true): Collect
596572
*/
597573
public function delete(): bool
598574
{
599-
if ($this->isVirtual() || $this->isView()) {
600-
$this->exists(false);
601-
$this->clear();
602-
return true;
603-
}
604-
605575
if ($this->isEmpty() || false === $this->trigger('BeforeDelete')) {
606576
return false;
607577
}
@@ -638,7 +608,14 @@ public static function create(array | object $data, array $allowField = [], bool
638608
{
639609
$model = new static();
640610

641-
$model->allowField($allowField)->replace($replace)->save($data, true);
611+
if ($model->isVirtual()) {
612+
if (!empty($data)) {
613+
// 初始化模型数据
614+
$model->initializeData($data, true);
615+
}
616+
} else {
617+
$model->allowField($allowField)->replace($replace)->save($data, true);
618+
}
642619

643620
return $model->fetchModel($model);
644621
}
@@ -655,6 +632,7 @@ public static function create(array | object $data, array $allowField = [], bool
655632
public static function update(array | object $data, $where = [], array $allowField = [], bool $refresh = false): Modelable
656633
{
657634
$model = new static();
635+
658636
$model->allowField($allowField)->exists(true)->save($data, $where, $refresh);
659637
return $model->fetchModel($model);
660638
}
@@ -670,11 +648,7 @@ public static function update(array | object $data, $where = [], array $allowFie
670648
public static function destroy($data, bool $force = false): bool
671649
{
672650
$model = new static();
673-
if ($model->isVirtual() || $model->isView()) {
674-
return true;
675-
}
676-
677-
$db = $model->db();
651+
$db = $model->db();
678652

679653
if (is_array($data) && key($data) !== 0) {
680654
$db->where($data);
@@ -819,9 +793,6 @@ public function __set(string $name, $value): void
819793
*/
820794
public function __isset(string $name): bool
821795
{
822-
if ($this->isView()) {
823-
return isset(self::$weakMap[$this]['data'][$name]);
824-
}
825796
return !is_null($this->get($name, false));
826797
}
827798

src/model/View.php

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

40-
if ($this->getOption('readonly')) {
41-
// 设置为只读视图模型
42-
$this->model()->asView(true);
43-
}
44-
4540
// 初始化模型
4641
$this->initData(!$with);
4742
}
@@ -413,10 +408,6 @@ protected function validate(array $data = [], array $allow = []): array
413408
*/
414409
public function save(): bool
415410
{
416-
if ($this->getOption('readonly')) {
417-
return false;
418-
}
419-
420411
// 根据映射关系转换为实际模型数据
421412
$data = $this->convertData();
422413
// 处理自动时间字段数据

src/model/concern/DbConnect.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public static function setDb($db)
3434
*/
3535
public function getQuery()
3636
{
37+
if ($this->isVirtual()) {
38+
throw new Exception('virtual model not support db query');
39+
}
40+
3741
$db = $this->initDb()->newQuery($this->getOption('query'));
3842

3943
if ($this->getOption('cache')) {
@@ -84,7 +88,7 @@ protected function getFields(?string $field = null)
8488
{
8589
$schema = $this->getOption('schema');
8690
if (empty($schema)) {
87-
if ($this->isView() || $this->isVirtual()) {
91+
if ($this->isVirtual()) {
8892
$schema = $this->getOption('type', []);
8993
} else {
9094
// 获取数据表信息
@@ -190,12 +194,7 @@ public static function withoutGlobalScope(?array $scope = null): Query
190194
public static function __callStatic($method, $args)
191195
{
192196
$model = new static();
193-
194-
if ($model->isVirtual()) {
195-
throw new Exception('virtual model not support db query');
196-
}
197-
198-
$db = $model->db();
197+
$db = $model->db();
199198

200199
if (!empty(self::$weakMap[$model]['autoRelation'])) {
201200
// 自动获取关联数据

tests/orm/ModelViewTest.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,7 @@ public function testViewModelRelation()
146146
$this->assertTrue(isset($viewModel->user_age));
147147
$this->assertFalse(isset($viewModel->not_exist));
148148

149-
// 测试视图模型写入限制
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); // 可写模型
149+
// 测试视图模型写入
157150
$viewModel->nickname = 'new_name';
158151
$viewModel->save();
159152
$viewModel = UserViewModel::find($user->id);

tests/orm/ModelVirtualTest.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace tests\orm;
55

66
use PHPUnit\Framework\TestCase;
7+
use think\db\exception\DbException;
78
use think\model\Virtual;
89

910
/**
@@ -22,18 +23,29 @@ public function testVirtualModelBasic()
2223
{
2324
// 测试数据操作
2425
$model = new VirtualModel();
25-
$data = ['name' => 'test', 'age' => 18];
26-
$this->assertTrue($model->save($data));
27-
$this->assertEquals($data, $model->getData());
26+
$model->name = 'test';
27+
$model->age = 18;
28+
$this->assertEquals('test', $model->name);
29+
$this->assertEquals(18, $model->age);
2830

2931
// 测试更新数据
30-
$updateData = ['age' => 20];
31-
$this->assertTrue($model->save($updateData));
32+
$model->age = 20;
3233
$this->assertEquals(20, $model->getData('age'));
3334

34-
// 测试删除数据
35-
$this->assertTrue($model->delete());
36-
$this->assertEmpty($model->getData());
35+
$this->expectException(DbException::class);
36+
$this->expectExceptionMessage("virtual model not support db query");
37+
$model->save();
38+
}
39+
40+
public function testVirtualDelete()
41+
{
42+
$model = new VirtualModel(['name' => 'test', 'age' => 18]);
43+
$this->assertEquals('test', $model->name);
44+
$this->assertEquals(18, $model->age);
45+
46+
$this->expectException(DbException::class);
47+
$this->expectExceptionMessage("virtual model not support db query");
48+
$model->delete();
3749
}
3850

3951
public function testVirtualModelCreate()

0 commit comments

Comments
 (0)