Skip to content

Commit d63949a

Browse files
committed
改进视图模型的save方法
1 parent 903e489 commit d63949a

File tree

3 files changed

+98
-16
lines changed

3 files changed

+98
-16
lines changed

src/Model.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ public function save(array | object $data = [], $where = [], bool $refresh = fal
385385
$this->setKey($db->getLastInsID());
386386
} elseif ($refresh) {
387387
// 刷新数据
388-
$data = $db->find()->getData();
389-
$this->data($data);
388+
$this->refresh();
390389
}
391390
$this->trigger($isUpdate ? 'AfterUpdate' : 'AfterInsert');
392391
$this->trigger('AfterWrite');

src/model/View.php

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ protected function initData()
6565
$this->$key = $this->fetchViewAttr($field, $data);
6666
}
6767
}
68+
// 标记数据存在
69+
$this->exists(true);
6870
}
6971

7072
/**
@@ -244,6 +246,7 @@ public function clear()
244246
foreach ($this->getEntityProperties() as $field) {
245247
$this->$field = null;
246248
}
249+
$this->exists(false);
247250
return $this;
248251
}
249252

@@ -456,23 +459,58 @@ protected function validate(array $data = [], array $allow = []): array
456459
return $data;
457460
}
458461

462+
/**
463+
* 设置数据是否存在.
464+
*
465+
* @param bool $exists
466+
*
467+
* @return $this
468+
*/
469+
public function exists(bool $exists = true)
470+
{
471+
return $this->setOption('exists', $exists);
472+
}
473+
474+
/**
475+
* 判断数据是否存在数据库.
476+
*
477+
* @return bool
478+
*/
479+
public function isExists(): bool
480+
{
481+
return $this->getOption('exists', false);
482+
}
483+
459484
/**
460485
* 保存模型实例数据.
461486
*
462-
* @param mixed $insert 是否强制新增 true为强制新增
487+
* @param array|object $data 数据
488+
* @param mixed $where 更新条件 true为强制新增
463489
* @param bool $refresh 是否刷新数据
464490
* @return bool
465491
*/
466-
public function save($insert = false, bool $refresh = false): bool
492+
public function save(array | object $data = [], $where = [], bool $refresh = false): bool
467493
{
494+
if ($data) {
495+
$this->data($data);
496+
}
497+
468498
// 根据映射关系转换为实际模型数据
469499
$data = $this->convertData();
470500
// 处理自动时间字段数据
471501
foreach ($this->model()->getAutoTimeFields() as $field) {
472502
unset($data[$field]);
473503
}
474504

475-
return $this->model()->save($data, $insert, $refresh);
505+
$result = $this->model()
506+
->exists($this->isExists())
507+
->save($data, $where, $refresh);
508+
509+
if ($result) {
510+
// 刷新数据
511+
$this->refresh();
512+
}
513+
return $result;
476514
}
477515

478516
/**
@@ -499,9 +537,8 @@ public function delete(): bool
499537
public static function create(array | object $data)
500538
{
501539
$entity = new static();
502-
$model = $entity->data($data)->save(true);
503-
// 刷新视图模型数据
504-
return $entity->refresh();
540+
$entity->exists(false)->save($data, true);
541+
return $entity;
505542
}
506543

507544
/**
@@ -514,9 +551,8 @@ public static function create(array | object $data)
514551
public static function update(array | object $data, $where = [])
515552
{
516553
$entity = new static();
517-
$model = $entity->data($data)->save($where, true);
518-
// 刷新视图模型数据
519-
return $entity->refresh();
554+
$entity->exists(true)->save($data, $where, true);
555+
return $entity;
520556
}
521557

522558
/**
@@ -540,10 +576,10 @@ public static function saveAll(iterable $dataSet, bool $replace = true): Collect
540576
$exists = false;
541577
}
542578
}
543-
$entity->model()->exists($exists);
579+
$entity->exists($exists);
544580
}
545-
$entity->data($data)->save(!$replace, true);
546-
$collection[] = $entity->refresh();
581+
$entity->save($data, !$replace, true);
582+
$collection[] = $entity;
547583
}
548584
return new Collection($collection);
549585
}

tests/orm/ModelViewTest.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class UserProfileModel extends Model
1515

1616
class UserViewModel extends View
1717
{
18+
public $id;
1819
public $nickname;
1920
public $test_name;
2021
public $user_age;
@@ -170,14 +171,21 @@ public function testEmptyViewModel()
170171
public function testViewModelWithRelation()
171172
{
172173
// 创建测试数据
174+
$user = new UserViewModel;
175+
$user->nickname = 'test3';
176+
$user->user_age = 28;
177+
$user->status = 1;
178+
$user->email = 'test3@example.com';
179+
$user->address = 'Test Address';
180+
$user->save();
181+
173182
$user = UserViewModel::create([
174183
'nickname' => 'test3',
175184
'user_age' => 28,
176185
'status' => 1,
177186
'email' => 'test3@example.com',
178187
'address' => 'Test Address',
179-
]);
180-
188+
]);
181189
// 加载关联数据
182190
$viewModel = UserViewModel::find($user->id);
183191

@@ -198,6 +206,45 @@ public function testViewModelWithRelation()
198206
$data = json_decode($json, true);
199207
$this->assertEquals('test3@example.com', $data['email']);
200208
$this->assertEquals('Test Address', $data['address']);
209+
210+
// 测试关联模型写入
211+
$viewModel->nickname = 'update_test3';
212+
$viewModel->email = 'update_test3@example.com';
213+
$viewModel->address = 'update_Test Address';
214+
$viewModel->save();
215+
216+
// 验证关联模型更新
217+
$updatedModel = UserViewModel::where('nickname', 'update_test3')->where('email', 'update_test3@example.com')->find();
218+
$this->assertEquals('update_test3', $updatedModel->nickname);
219+
$this->assertEquals('update_test3@example.com', $updatedModel->email);
220+
221+
// 批量创建或更新数据
222+
$dataset = [
223+
['nickname' => 'test4',
224+
'user_age' => 20,
225+
'status' => 1,
226+
'email' => 'test4@example.com',
227+
'address' => 'Address4',],
228+
['id' => 1,
229+
'nickname' => 'test3',
230+
'user_age' => 18,
231+
'status' => 1,
232+
'email' => 'test3@example.com',
233+
'address' => 'Test Address',],
234+
];
235+
$list = UserViewModel::saveAll($dataset);
236+
foreach ($list as $key => $user) {
237+
$this->assertEquals($dataset[$key]['nickname'], $user->nickname);
238+
$this->assertEquals($dataset[$key]['email'], $user->email);
239+
}
240+
$viewModel = UserViewModel::find(1);
241+
$this->assertEquals('test3', $viewModel->nickname);
242+
$this->assertEquals('test_test3', $viewModel->test_name);
243+
$this->assertEquals(18, $viewModel->user_age);
244+
245+
$user = UserViewModel::update(['nickname' => 'new nickname'], ['id' => 1]);
246+
$this->assertEquals('new nickname', $user->nickname);
247+
201248
}
202249

203250
public function testViewModelWithoutRelation()

0 commit comments

Comments
 (0)