本文实例讲述了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;
    }
}

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!