@@ -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 ();
0 commit comments