Skip to content

Commit d05e89e

Browse files
committed
改进虚拟模型
1 parent ad3258a commit d05e89e

File tree

4 files changed

+56
-45
lines changed

4 files changed

+56
-45
lines changed

src/Model.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -509,16 +509,6 @@ public function isChange(string $name): bool
509509
return $this->getData($name) !== $this->getOrigin($name);
510510
}
511511

512-
/**
513-
* 是否为虚拟模型(不能查询和写入).
514-
*
515-
* @return bool
516-
*/
517-
public function isVirtual(): bool
518-
{
519-
return false;
520-
}
521-
522512
/**
523513
* 刷新模型数据.
524514
*
@@ -607,16 +597,7 @@ public function delete(): bool
607597
public static function create(array | object $data, array $allowField = [], bool $replace = false): Modelable
608598
{
609599
$model = new static();
610-
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-
}
619-
600+
$model->allowField($allowField)->replace($replace)->save($data, true);
620601
return $model->fetchModel($model);
621602
}
622603

@@ -632,7 +613,6 @@ public static function create(array | object $data, array $allowField = [], bool
632613
public static function update(array | object $data, $where = [], array $allowField = [], bool $refresh = false): Modelable
633614
{
634615
$model = new static();
635-
636616
$model->allowField($allowField)->exists(true)->save($data, $where, $refresh);
637617
return $model->fetchModel($model);
638618
}

src/model/Virtual.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
namespace think\model;
1515

16+
use think\db\exception\DbException as Exception;
1617
use think\Model;
18+
use think\model\contract\Modelable;
1719

1820
/**
1921
* Class Virtual.
@@ -22,12 +24,49 @@
2224
abstract class Virtual extends Model
2325
{
2426
/**
25-
* 设置为虚拟模型.
27+
* 写入数据.
2628
*
27-
* @return bool
29+
* @param array|object $data 数据
30+
* @param array $allowField 允许字段
31+
* @param bool $replace 使用Replace
32+
* @return Modelable
2833
*/
29-
public function isVirtual(): bool
34+
public static function create(array | object $data, array $allowField = [], bool $replace = false): Modelable
3035
{
31-
return true;
36+
$model = new static();
37+
38+
if (!empty($data)) {
39+
// 初始化模型数据
40+
$model->initializeData($data, true);
41+
}
42+
43+
return $model->fetchModel($model);
3244
}
45+
46+
/**
47+
* 获取Db对象实例.
48+
* @return Query
49+
*/
50+
public function getQuery()
51+
{
52+
throw new Exception('virtual model not support db query');
53+
}
54+
55+
/**
56+
* 获取数据表字段类型列表(或某个字段的类型).
57+
*
58+
* @param string|null $field 字段名
59+
*
60+
* @return array|string
61+
*/
62+
protected function getFields(?string $field = null)
63+
{
64+
$schema = $this->getOption('schema', []);
65+
66+
if ($field) {
67+
return $schema[$field] ?? null;
68+
}
69+
70+
return $schema;
71+
}
3372
}

src/model/concern/Attribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ trait Attribute
4040
*
4141
* @return void
4242
*/
43-
private function initializeData(array | object $data, bool $fromSave = false)
43+
protected function initializeData(array | object $data, bool $fromSave = false)
4444
{
4545
// 分析数据
4646
$data = $this->parseData($data);

src/model/concern/DbConnect.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace think\model\concern;
1515

1616
use think\db\BaseQuery as Query;
17-
use think\db\exception\DbException as Exception;
1817
use think\facade\Db;
1918

2019
/**
@@ -34,10 +33,6 @@ public static function setDb($db)
3433
*/
3534
public function getQuery()
3635
{
37-
if ($this->isVirtual()) {
38-
throw new Exception('virtual model not support db query');
39-
}
40-
4136
$db = $this->initDb()->newQuery($this->getOption('query'));
4237

4338
if ($this->getOption('cache')) {
@@ -88,20 +83,17 @@ protected function getFields(?string $field = null)
8883
{
8984
$schema = $this->getOption('schema');
9085
if (empty($schema)) {
91-
if ($this->isVirtual()) {
92-
$schema = $this->getOption('type', []);
93-
} else {
94-
// 获取数据表信息
95-
$db = $this->initDb();
96-
$fields = $db->getFieldsType();
97-
$schema = array_merge($fields, $this->getOption('type', []));
98-
// 获取主键和自增字段
99-
if (!$this->getOption('pk')) {
100-
$this->setOption('pk', $db->getPk());
101-
}
102-
if (!$this->getOption('autoInc')) {
103-
$this->setOption('autoInc', $db->getAutoInc());
104-
}
86+
// 获取数据表信息
87+
$db = $this->initDb();
88+
$fields = $db->getFieldsType();
89+
$schema = array_merge($fields, $this->getOption('type', []));
90+
// 获取主键和自增字段
91+
if (!$this->getOption('pk')) {
92+
$this->setOption('pk', $db->getPk());
93+
}
94+
95+
if (!$this->getOption('autoInc')) {
96+
$this->setOption('autoInc', $db->getAutoInc());
10597
}
10698

10799
$this->setOption('schema', $schema);

0 commit comments

Comments
 (0)