@@ -388,20 +388,22 @@ public function isExists(): bool
388
388
* 保存当前数据对象
389
389
* @access public
390
390
* @param array $data 数据
391
- * @param array $where 更新条件
392
391
* @param string $sequence 自增序列名
393
392
* @return bool
394
393
*/
395
- public function save (array $ data = [], array $ where = [], string $ sequence = null ): bool
394
+ public function save (array $ data = [], string $ sequence = null ): bool
396
395
{
397
- $ this ->checkBeforeSave ($ data , $ where );
396
+ // 数据对象赋值
397
+ foreach ($ data as $ key => $ value ) {
398
+ $ this ->setAttr ($ key , $ value , $ data );
399
+ }
398
400
399
401
// 事件回调
400
402
if (false === $ this ->trigger ('before_write ' )) {
401
403
return false ;
402
404
}
403
405
404
- $ result = $ this ->exists ? $ this ->updateData ($ where ) : $ this ->insertData ($ sequence );
406
+ $ result = $ this ->exists ? $ this ->updateData () : $ this ->insertData ($ sequence );
405
407
406
408
if (false === $ result ) {
407
409
return false ;
@@ -417,28 +419,6 @@ public function save(array $data = [], array $where = [], string $sequence = nul
417
419
return true ;
418
420
}
419
421
420
- /**
421
- * 写入之前检查数据
422
- * @access protected
423
- * @param array $data 数据
424
- * @param array $where 保存条件
425
- * @return void
426
- */
427
- protected function checkBeforeSave (array $ data , $ where ): void
428
- {
429
- if (!empty ($ data )) {
430
- // 数据对象赋值
431
- foreach ($ data as $ key => $ value ) {
432
- $ this ->setAttr ($ key , $ value , $ data );
433
- }
434
-
435
- if (!empty ($ where )) {
436
- $ this ->exists = true ;
437
- $ this ->updateWhere = $ where ;
438
- }
439
- }
440
- }
441
-
442
422
/**
443
423
* 检查数据是否允许写入
444
424
* @access protected
@@ -478,13 +458,13 @@ protected function checkAllowFields(array $append = []): array
478
458
/**
479
459
* 保存写入数据
480
460
* @access protected
481
- * @param array $where 保存条件
482
461
* @return bool
483
462
*/
484
- protected function updateData (array $ where ): bool
463
+ protected function updateData (): bool
485
464
{
486
465
// 自动更新
487
466
$ auto = array_merge ($ this ->auto , $ this ->update );
467
+
488
468
$ this ->autoCompleteData ($ auto );
489
469
490
470
// 事件回调
@@ -509,10 +489,6 @@ protected function updateData(array $where): bool
509
489
$ this ->data [$ this ->updateTime ] = $ data [$ this ->updateTime ];
510
490
}
511
491
512
- if (empty ($ where ) && !empty ($ this ->updateWhere )) {
513
- $ where = $ this ->updateWhere ;
514
- }
515
-
516
492
// 检查允许字段
517
493
$ allowFields = $ this ->checkAllowFields ($ auto );
518
494
@@ -535,14 +511,18 @@ protected function updateData(array $where): bool
535
511
536
512
if (!empty ($ array )) {
537
513
$ where = $ array ;
514
+ } else {
515
+ $ where = $ this ->updateWhere ;
538
516
}
539
517
540
518
foreach ((array ) $ this ->relationWrite as $ name => $ val ) {
541
- if (is_array ($ val )) {
542
- foreach ($ val as $ key ) {
543
- if (isset ($ data [$ key ])) {
544
- unset($ data [$ key ]);
545
- }
519
+ if (!is_array ($ val )) {
520
+ continue ;
521
+ }
522
+
523
+ foreach ($ val as $ key ) {
524
+ if (isset ($ data [$ key ])) {
525
+ unset($ data [$ key ]);
546
526
}
547
527
}
548
528
}
@@ -584,6 +564,7 @@ protected function insertData(string $sequence = null): bool
584
564
{
585
565
// 自动写入
586
566
$ auto = array_merge ($ this ->auto , $ this ->insert );
567
+
587
568
$ this ->autoCompleteData ($ auto );
588
569
589
570
// 时间戳自动写入
@@ -680,7 +661,7 @@ public function saveAll(array $dataSet, bool $replace = true): Collection
680
661
681
662
foreach ($ dataSet as $ key => $ data ) {
682
663
if ($ this ->exists || (!empty ($ auto ) && isset ($ data [$ pk ]))) {
683
- $ result [$ key ] = self ::update ($ data , [], $ this ->field );
664
+ $ result [$ key ] = self ::update ($ data , $ this ->field );
684
665
} else {
685
666
$ result [$ key ] = self ::create ($ data , $ this ->field , $ this ->replace );
686
667
}
@@ -794,15 +775,15 @@ public static function create(array $data, array $field = [], bool $replace = fa
794
775
* @param array $field 允许字段
795
776
* @return static
796
777
*/
797
- public static function update (array $ data , array $ where = [], array $ field = [])
778
+ public static function update (array $ data , array $ field = [])
798
779
{
799
780
$ model = new static ();
800
781
801
782
if (!empty ($ field )) {
802
783
$ model ->allowField ($ field );
803
784
}
804
785
805
- $ model ->isUpdate (true )->save ($ data, $ where );
786
+ $ model ->isUpdate (true )->save ($ data );
806
787
807
788
return $ model ;
808
789
}
0 commit comments