thinkPHP事务操作简单案例分析
吾爱主题
阅读:99
2021-09-07 16:01:00
评论:0
本文实例讲述了thinkPHP事务操作。分享给大家供大家参考,具体如下:
1.说明
?1 2 3 4 5 6 7 8 9 10 | Db::startTrans(); try { 操作; 对象->save(); Db::commit(); } catch (Exception $ex ) { Db::rollback(); throw $ex ; } |
2.案例
?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 | private function createOrderByTrans( $snap ) { Db::startTrans(); try { $orderNo = $this ->makeOrderNo(); $order = new OrderModel(); $order ->user_id = $this ->uid; $order ->order_no = $orderNo ; $order ->total_price = $snap [ 'orderPrice' ]; $order ->total_count = $snap [ 'totalCount' ]; $order ->snap_img = $snap [ 'snapImg' ]; $order ->snap_name = $snap [ 'snapName' ]; $order ->snap_address = $snap [ 'snapAddress' ]; $order ->snap_items = json_encode( $snap [ 'pStatus' ]); $order ->save(); $orderID = $order ->id; $create_time = $order ->create_time; foreach ( $this ->oProducts as & $p ) { $p [ 'order_id' ] = $orderID ; } $orderProduct = new OrderProduct(); $orderProduct ->saveAll( $this ->oProducts); Db::commit(); return [ 'order_no' => $orderNo , 'order_id' => $orderID , 'create_time' => $create_time ]; } catch (Exception $ex ) { Db::rollback(); throw $ex ; } } |
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/fujian9544/article/details/94597040
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。