@@ -42,6 +42,12 @@ abstract class Model implements JsonSerializable, ArrayAccess
42
42
*/
43
43
private $ force = false ;
44
44
45
+ /**
46
+ * 是否Replace
47
+ * @var bool
48
+ */
49
+ private $ replace = false ;
50
+
45
51
/**
46
52
* 更新条件
47
53
* @var array
@@ -96,12 +102,6 @@ abstract class Model implements JsonSerializable, ArrayAccess
96
102
*/
97
103
protected static $ initialized = [];
98
104
99
- /**
100
- * 是否从主库读取(主从分布式有效)
101
- * @var array
102
- */
103
- protected static $ readMaster ;
104
-
105
105
/**
106
106
* 查询对象实例
107
107
* @var Query
@@ -210,21 +210,6 @@ public function newInstance(array $data = [], bool $isUpdate = false, $where = n
210
210
return (new static ($ data ))->isUpdate ($ isUpdate , $ where );
211
211
}
212
212
213
- /**
214
- * 是否从主库读取数据(主从分布有效)
215
- * @access public
216
- * @param bool $all 是否所有模型有效
217
- * @return $this
218
- */
219
- public function readMaster (bool $ all = false )
220
- {
221
- $ model = $ all ? '* ' : static ::class;
222
-
223
- static ::$ readMaster [$ model ] = true ;
224
-
225
- return $ this ;
226
- }
227
-
228
213
/**
229
214
* 创建模型的查询对象
230
215
* @access protected
@@ -238,11 +223,7 @@ protected function buildQuery(): Query
238
223
$ query ->model ($ this )
239
224
->name ($ this ->name )
240
225
->json ($ this ->json , $ this ->jsonAssoc )
241
- ->setFieldType ($ this ->type );
242
-
243
- if (isset (static ::$ readMaster ['* ' ]) || isset (static ::$ readMaster [static ::class])) {
244
- $ query ->master (true );
245
- }
226
+ ->setFieldType ($ this ->schema );
246
227
247
228
// 设置当前数据表和模型名
248
229
if (!empty ($ this ->table )) {
@@ -369,6 +350,18 @@ public function isForce(): bool
369
350
return $ this ->force ;
370
351
}
371
352
353
+ /**
354
+ * 新增数据是否使用Replace
355
+ * @access public
356
+ * @param bool $replace
357
+ * @return $this
358
+ */
359
+ public function replace (bool $ replace = true )
360
+ {
361
+ $ this ->replace = $ replace ;
362
+ return $ this ;
363
+ }
364
+
372
365
/**
373
366
* 设置数据是否存在
374
367
* @access public
@@ -395,17 +388,22 @@ public function isExists(): bool
395
388
* 保存当前数据对象
396
389
* @access public
397
390
* @param array $data 数据
398
- * @param array $where 更新条件
399
391
* @param string $sequence 自增序列名
400
392
* @return bool
401
393
*/
402
- public function save (array $ data = [], array $ where = [], string $ sequence = null ): bool
394
+ public function save (array $ data = [], string $ sequence = null ): bool
403
395
{
404
- if (!$ this ->checkBeforeSave ($ data , $ where )) {
396
+ // 数据对象赋值
397
+ foreach ($ data as $ key => $ value ) {
398
+ $ this ->setAttr ($ key , $ value , $ data );
399
+ }
400
+
401
+ // 事件回调
402
+ if (false === $ this ->trigger ('before_write ' )) {
405
403
return false ;
406
404
}
407
405
408
- $ result = $ this ->exists ? $ this ->updateData ($ where ) : $ this ->insertData ($ sequence );
406
+ $ result = $ this ->exists ? $ this ->updateData () : $ this ->insertData ($ sequence );
409
407
410
408
if (false === $ result ) {
411
409
return false ;
@@ -421,39 +419,6 @@ public function save(array $data = [], array $where = [], string $sequence = nul
421
419
return true ;
422
420
}
423
421
424
- /**
425
- * 写入之前检查数据
426
- * @access protected
427
- * @param array $data 数据
428
- * @param array $where 保存条件
429
- * @return bool
430
- */
431
- protected function checkBeforeSave (array $ data , $ where ): bool
432
- {
433
- if (!empty ($ data )) {
434
-
435
- // 数据对象赋值
436
- foreach ($ data as $ key => $ value ) {
437
- $ this ->setAttr ($ key , $ value , $ data );
438
- }
439
-
440
- if (!empty ($ where )) {
441
- $ this ->exists = true ;
442
- $ this ->updateWhere = $ where ;
443
- }
444
- }
445
-
446
- // 数据自动完成
447
- $ this ->autoCompleteData ($ this ->auto );
448
-
449
- // 事件回调
450
- if (false === $ this ->trigger ('before_write ' )) {
451
- return false ;
452
- }
453
-
454
- return true ;
455
- }
456
-
457
422
/**
458
423
* 检查数据是否允许写入
459
424
* @access protected
@@ -464,10 +429,14 @@ protected function checkAllowFields(array $append = []): array
464
429
{
465
430
// 检测字段
466
431
if (empty ($ this ->field )) {
467
- $ query = $ this ->db ();
468
- $ table = $ this ->table ?: $ query ->getTable ();
432
+ if ($ this ->schema ) {
433
+ $ this ->field = array_keys ($ this ->schema );
434
+ } else {
435
+ $ query = $ this ->db ();
436
+ $ table = $ this ->table ?: $ query ->getTable ();
469
437
470
- $ this ->field = $ query ->getConnection ()->getTableFields ($ table );
438
+ $ this ->field = $ query ->getConnection ()->getTableFields ($ table );
439
+ }
471
440
472
441
$ field = $ this ->field ;
473
442
} else {
@@ -489,13 +458,14 @@ protected function checkAllowFields(array $append = []): array
489
458
/**
490
459
* 保存写入数据
491
460
* @access protected
492
- * @param array $where 保存条件
493
461
* @return bool
494
462
*/
495
- protected function updateData (array $ where ): bool
463
+ protected function updateData (): bool
496
464
{
497
465
// 自动更新
498
- $ this ->autoCompleteData ($ this ->update );
466
+ $ auto = array_merge ($ this ->auto , $ this ->update );
467
+
468
+ $ this ->autoCompleteData ($ auto );
499
469
500
470
// 事件回调
501
471
if (false === $ this ->trigger ('before_update ' )) {
@@ -519,12 +489,8 @@ protected function updateData(array $where): bool
519
489
$ this ->data [$ this ->updateTime ] = $ data [$ this ->updateTime ];
520
490
}
521
491
522
- if (empty ($ where ) && !empty ($ this ->updateWhere )) {
523
- $ where = $ this ->updateWhere ;
524
- }
525
-
526
492
// 检查允许字段
527
- $ allowFields = $ this ->checkAllowFields (array_merge ( $ this -> auto , $ this -> update ) );
493
+ $ allowFields = $ this ->checkAllowFields ($ auto );
528
494
529
495
// 保留主键数据
530
496
foreach ($ this ->data as $ key => $ val ) {
@@ -545,14 +511,18 @@ protected function updateData(array $where): bool
545
511
546
512
if (!empty ($ array )) {
547
513
$ where = $ array ;
514
+ } else {
515
+ $ where = $ this ->updateWhere ;
548
516
}
549
517
550
518
foreach ((array ) $ this ->relationWrite as $ name => $ val ) {
551
- if (is_array ($ val )) {
552
- foreach ($ val as $ key ) {
553
- if (isset ($ data [$ key ])) {
554
- unset($ data [$ key ]);
555
- }
519
+ if (!is_array ($ val )) {
520
+ continue ;
521
+ }
522
+
523
+ foreach ($ val as $ key ) {
524
+ if (isset ($ data [$ key ])) {
525
+ unset($ data [$ key ]);
556
526
}
557
527
}
558
528
}
@@ -593,7 +563,9 @@ protected function updateData(array $where): bool
593
563
protected function insertData (string $ sequence = null ): bool
594
564
{
595
565
// 自动写入
596
- $ this ->autoCompleteData ($ this ->insert );
566
+ $ auto = array_merge ($ this ->auto , $ this ->insert );
567
+
568
+ $ this ->autoCompleteData ($ auto );
597
569
598
570
// 时间戳自动写入
599
571
$ this ->checkTimeStampWrite ();
@@ -603,7 +575,7 @@ protected function insertData(string $sequence = null): bool
603
575
}
604
576
605
577
// 检查允许字段
606
- $ allowFields = $ this ->checkAllowFields (array_merge ( $ this -> auto , $ this -> insert ) );
578
+ $ allowFields = $ this ->checkAllowFields ($ auto );
607
579
608
580
$ db = $ this ->db ();
609
581
$ db ->startTrans ();
@@ -644,52 +616,6 @@ protected function insertData(string $sequence = null): bool
644
616
}
645
617
}
646
618
647
- /**
648
- * 字段值(延迟)增长
649
- * @access public
650
- * @param string $field 字段名
651
- * @param integer $step 增长值
652
- * @param integer $lazyTime 延时时间(s)
653
- * @return integer|true
654
- * @throws Exception
655
- */
656
- public function setInc (string $ field , int $ step = 1 , int $ lazyTime = 0 )
657
- {
658
- // 读取更新条件
659
- $ where = $ this ->getWhere ();
660
-
661
- $ result = $ this ->where ($ where )->setInc ($ field , $ step , $ lazyTime );
662
-
663
- if (true !== $ result ) {
664
- $ this ->data [$ field ] += $ step ;
665
- }
666
-
667
- return $ result ;
668
- }
669
-
670
- /**
671
- * 字段值(延迟)减少
672
- * @access public
673
- * @param string $field 字段名
674
- * @param integer $step 减少值
675
- * @param integer $lazyTime 延时时间(s)
676
- * @return integer|true
677
- * @throws Exception
678
- */
679
- public function setDec (string $ field , int $ step = 1 , int $ lazyTime = 0 )
680
- {
681
- // 读取更新条件
682
- $ where = $ this ->getWhere ();
683
-
684
- $ result = $ this ->where ($ where )->setDec ($ field , $ step , $ lazyTime );
685
-
686
- if (true !== $ result ) {
687
- $ this ->data [$ field ] -= $ step ;
688
- }
689
-
690
- return $ result ;
691
- }
692
-
693
619
/**
694
620
* 获取当前的更新条件
695
621
* @access protected
@@ -735,9 +661,9 @@ public function saveAll(array $dataSet, bool $replace = true): Collection
735
661
736
662
foreach ($ dataSet as $ key => $ data ) {
737
663
if ($ this ->exists || (!empty ($ auto ) && isset ($ data [$ pk ]))) {
738
- $ result [$ key ] = self ::update ($ data , [], $ this ->field );
664
+ $ result [$ key ] = self ::update ($ data , $ this ->field );
739
665
} else {
740
- $ result [$ key ] = self ::create ($ data , $ this ->field );
666
+ $ result [$ key ] = self ::create ($ data , $ this ->field , $ this -> replace );
741
667
}
742
668
}
743
669
@@ -825,17 +751,18 @@ public function auto(array $fields)
825
751
* @access public
826
752
* @param array $data 数据数组
827
753
* @param array $field 允许字段
754
+ * @param bool $replace 使用Replace
828
755
* @return static
829
756
*/
830
- public static function create (array $ data , array $ field = []): Model
757
+ public static function create (array $ data , array $ field = [], bool $ replace = false ): Model
831
758
{
832
759
$ model = new static ();
833
760
834
761
if (!empty ($ field )) {
835
762
$ model ->allowField ($ field );
836
763
}
837
764
838
- $ model ->isUpdate (false )->save ($ data );
765
+ $ model ->isUpdate (false )->replace ( $ replace )-> save ($ data );
839
766
840
767
return $ model ;
841
768
}
@@ -844,19 +771,18 @@ public static function create(array $data, array $field = []): Model
844
771
* 更新数据
845
772
* @access public
846
773
* @param array $data 数据数组
847
- * @param array $where 更新条件
848
774
* @param array $field 允许字段
849
775
* @return static
850
776
*/
851
- public static function update (array $ data , array $ where = [], array $ field = [])
777
+ public static function update (array $ data , array $ field = [])
852
778
{
853
779
$ model = new static ();
854
780
855
781
if (!empty ($ field )) {
856
782
$ model ->allowField ($ field );
857
783
}
858
784
859
- $ model ->isUpdate (true )->save ($ data, $ where );
785
+ $ model ->isUpdate (true )->save ($ data );
860
786
861
787
return $ model ;
862
788
}
0 commit comments