@@ -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 }
0 commit comments