thinkPHP事务操作简单案例分析
发布:smiling 来源: PHP粉丝网 添加日期:2022-01-08 15:41:54 浏览: 评论:0
这篇文章主要介绍了thinkPHP事务操作,结合简单案例形式分析了thinkPHP事务操作的基本原理与使用方法,需要的朋友可以参考下。
本文实例讲述了thinkPHP事务操作,分享给大家供大家参考,具体如下:
1.说明
- Db::startTrans();
- try{
- 操作;
- 对象->save();
- Db::commit();
- }
- catch (Exception $ex) {
- Db::rollback();
- throw $ex;
- }
2.案例
- 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;
- }
- }
Tags: thinkPHP事务操作
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)