复制代码
1
2thinkphp5
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122可以不用 try {} catch (Exception $e) {} 可以直接使用 Db::startTrans(); Db::commit(); Db::rollback(); if (!$updateMoney) { Db::rollback(); return V(0, '出错,请重试'); } 注意 使用tp5事务时 不要用db() 要用Db::name 或者 Db::table // 开启事务 Db::startTrans(); try { // 开启事务 Db::startTrans(); //写在这个位置也可以 //任意一个表写入失败都会抛出异常: //比如加一个判断 if (false) { throw new Exception('操作失败'); //抛出错误 } //根据 订单id 和门店 id 去判断 有没有分配分拣员 $where_a['id'] = $post_data['order_id']; // $where_a['store_id'] = $post_data['store_id']; $find_storer = Db::name('shopro_order')->where($where_a)->value('storer_id'); if(!$find_storer){ //没有分配的分拣员 //先去判断有没有分拣员上班 $storer_info = $this->get_assign_storer(); if(!$storer_info){ throw new Exception('无分拣员接单'); } //第一步要去获取分拣员的数据 读取缓存数据 $storer_id = $storer_info['id']; //分拣员id //先去分配分拣员 $where_storer['order_id'] = $post_data['order_id']; $update_storer['storer_id'] = $storer_id; $re['storer'] = Db('shopro_order_son')->where($where_storer)->update($update_storer); //主表 $where_order['id'] = $post_data['order_id']; $update_order['storer_id'] = $storer_id; $update_order['storer_state'] = 1; $update_order['storer_assign_time'] = date('Y-m-d H:i:s',time()); $re['order'] = Db('shopro_order')->where($where_order)->update($update_order); } //去修改子订单表 clz_shopro_order_son $where_son['order_id'] = $post_data['order_id']; $where_son['store_id'] = $post_data['store_id']; $update_son['store_state'] = 2; $update_son['storer_state'] = 1; $update_son['store_stockup_time'] = date('Y-m-d H:i:s',time()); $re['son'] = Db('shopro_order_son')->where($where_son)->update($update_son); //任意一个表写入失败都会抛出异常: if (in_array('0', $re)) { // if ($res1==0 || $res2==0 ||$res3 == 0) { throw new Exception('操作失败');//抛出错误 } // 提交事务 Db::commit(); $this->return_data(1, '操作成功', true); } catch (Exception $e) { // 回滚事务 如获取到异常信息,对所有表的删、改、写操作,都会回滚至操作前的状态: Db::rollback(); $msg = $e->getMessage(); $this->return_data(0, $msg, true); } 、、、、、、、、 try { //开启事物 Db::startTrans(); $res1 = Db:name('frieslandtravel_apply')->insert($add_apply); $res2 = Db:name('frieslandtravel_apply_son')->insert($add_apply_son); //任意一个表写入失败都会抛出异常: if ($res1==0 || $res2==0) { throw new Exception('操作失败');//抛出错误 } // 提交事务 Db::commit(); exit_json( init_err(0, '操作成功') ); } catch (Exception $e) { // 回滚事务 如获取到异常信息,对所有表的删、改、写操作,都会回滚至操作前的状态: Db::rollback(); exit_json( init_err(1, '操作失败') ); } if ( !function_exists('init_err') ) { function init_err($err=1, $msg='未知错误', $data=null) { return [ 'err' => $err, 'msg' => $msg, 'data' => $data, ]; } } if ( !function_exists('exit_json') ) { /** * Desc:输出json字符串 * Date:2021/1/31 14:55 * Author: guanzy@huilan-online.com * -------------------------------- * @param $data * -------------------------------- */ function exit_json($data) { exit( json_encode($data, 256) ); } }
最后
以上就是虚幻路人最近收集整理的关于thinkphp5 事务回滚的全部内容,更多相关thinkphp5内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复