@@ -533,18 +533,31 @@ protected function updateData($where)
533
533
}
534
534
}
535
535
536
- // 模型更新
537
- $ result = $ this -> db ( false )-> where ( $ where )-> strict ( false )-> field ( $ allowFields )-> update ( $ data );
536
+ $ db = $ this -> db ( false );
537
+ $ db -> startTrans ( );
538
538
539
- // 关联更新
540
- if (isset ($ this ->relationWrite )) {
541
- $ this ->autoRelationUpdate ();
542
- }
539
+ try {
540
+ // 模型更新
541
+ $ result = $ db ->where ($ where )
542
+ ->strict (false )
543
+ ->field ($ allowFields )
544
+ ->update ($ data );
545
+
546
+ // 关联更新
547
+ if (isset ($ this ->relationWrite )) {
548
+ $ this ->autoRelationUpdate ();
549
+ }
543
550
544
- // 更新回调
545
- $ this ->trigger ('after_update ' );
551
+ $ db ->commit ();
546
552
547
- return $ result ;
553
+ // 更新回调
554
+ $ this ->trigger ('after_update ' );
555
+
556
+ return $ result ;
557
+ } catch (\Exception $ e ) {
558
+ $ db ->rollback ();
559
+ throw $ e ;
560
+ }
548
561
}
549
562
550
563
/**
@@ -568,31 +581,43 @@ protected function insertData($sequence)
568
581
// 检查允许字段
569
582
$ allowFields = $ this ->checkAllowFields (array_merge ($ this ->auto , $ this ->insert ));
570
583
571
- $ result = $ this ->db (false )->strict (false )->field ($ allowFields )->insert ($ this ->data );
584
+ $ db = $ this ->db (false );
585
+ $ db ->startTrans ();
572
586
573
- // 获取自动增长主键
574
- if ($ result && $ insertId = $ this ->db (false )->getLastInsID ($ sequence )) {
575
- $ pk = $ this ->getPk ();
587
+ try {
588
+ $ result = $ db ->strict (false )
589
+ ->field ($ allowFields )
590
+ ->insert ($ this ->data );
591
+
592
+ // 获取自动增长主键
593
+ if ($ result && $ insertId = $ db ->getLastInsID ($ sequence )) {
594
+ $ pk = $ this ->getPk ();
576
595
577
- foreach ((array ) $ pk as $ key ) {
578
- if (!isset ($ this ->data [$ key ]) || '' == $ this ->data [$ key ]) {
579
- $ this ->data [$ key ] = $ insertId ;
596
+ foreach ((array ) $ pk as $ key ) {
597
+ if (!isset ($ this ->data [$ key ]) || '' == $ this ->data [$ key ]) {
598
+ $ this ->data [$ key ] = $ insertId ;
599
+ }
580
600
}
581
601
}
582
- }
583
602
584
- // 关联写入
585
- if (isset ($ this ->relationWrite )) {
586
- $ this ->autoRelationInsert ();
587
- }
603
+ // 关联写入
604
+ if (isset ($ this ->relationWrite )) {
605
+ $ this ->autoRelationInsert ();
606
+ }
588
607
589
- // 标记为更新
590
- $ this ->isUpdate = true ;
608
+ $ db ->commit ();
591
609
592
- // 新增回调
593
- $ this ->trigger ( ' after_insert ' ) ;
610
+ // 标记为更新
611
+ $ this ->isUpdate = true ;
594
612
595
- return $ result ;
613
+ // 新增回调
614
+ $ this ->trigger ('after_insert ' );
615
+
616
+ return $ result ;
617
+ } catch (\Exception $ e ) {
618
+ $ db ->rollback ();
619
+ throw $ e ;
620
+ }
596
621
}
597
622
598
623
/**
@@ -738,21 +763,31 @@ public function delete()
738
763
// 读取更新条件
739
764
$ where = $ this ->getWhere ();
740
765
741
- // 删除当前模型数据
742
- $ result = $ this -> db ( false )-> where ( $ where )-> delete ();
766
+ $ db = $ this -> db ( false );
767
+ $ db -> startTrans ();
743
768
744
- // 关联删除
745
- if (!empty ($ this ->relationWrite )) {
746
- $ this ->autoRelationDelete ();
747
- }
769
+ try {
770
+ // 删除当前模型数据
771
+ $ result = $ db ->where ($ where )->delete ();
772
+
773
+ // 关联删除
774
+ if (!empty ($ this ->relationWrite )) {
775
+ $ this ->autoRelationDelete ();
776
+ }
748
777
749
- $ this -> trigger ( ' after_delete ' );
778
+ $ db -> commit ( );
750
779
751
- // 清空数据
752
- $ this ->data = [];
753
- $ this ->origin = [];
780
+ $ this ->trigger ('after_delete ' );
754
781
755
- return $ result ;
782
+ // 清空数据
783
+ $ this ->data = [];
784
+ $ this ->origin = [];
785
+
786
+ return $ result ;
787
+ } catch (\Exception $ e ) {
788
+ $ db ->rollback ();
789
+ throw $ e ;
790
+ }
756
791
}
757
792
758
793
/**
0 commit comments