zoukankan      html  css  js  c++  java
  • ThinkPHP 3.2.2 事务

    手册里说得非常清楚 :
    5.3.19 事务支持
    ThinkPHP提供了单数据库的事务支持,如果要在应用逻辑中使用事务,可以参考下面的方法:
    启动事务:
    PHP代码
    $User->startTrans()   
    提交事务:
    PHP代码
    $User->commit()   
    事务回滚:
    PHP代码
    $User->rollback()  
     
    事务是针对数据库本身的,所以可以跨模型操作的 。
    例如:
    PHP代码
    //  在User模型中启动事务   
    $User->startTrans()    
    // 进行相关的业务逻辑操作   
    $Info = M("Info"); // 实例化Info对象   
    $Info->save($User); // 保存用户信息   
    if (操作成功){   
    // 提交事务   
    $User->commit()    
    }else{   
    // 事务回滚   
    $User->rollback()    
    }
     1 <?php
     2 namespace SMSController;
     3 use ThinkController;
     4 class IndexController extends Controller {
     5     public function index(){
     6         $data['operator'] = 'Testss';
     7         M()->startTrans();
     8         $result = M('feehistory')->add($data);
     9         $result1 = $result2 = true;
    10         if(!empty($result)){
    11             $regdelData['level'] = '111';
    12             $result1 = M('regdel')->add($regdelData);
    13 
    14             $regData['level'] = '101';
    15             $result2 = M('reg')->where("registryCode='13693536752-SJB-HUAX-12345678'")->save($regData);
    16         }
    17         if(!empty($result) && !empty($result1) && !empty($result2) ){
    18             M()->commit();    
    19             //$this->success('事物提交',__ROOT__);
    20             echo '事物提交';
    21         }else{
    22             M()->rollback();
    23             //$this->error('事物回滚',__ROOT__);
    24             echo '事物回滚';
    25         }
    26     }
    27 }

    28 ?> 

  • 相关阅读:
    翻译:让网络更快一些——最小化浏览器中的回流(reflow)
    ArcGIS API for flex 3.1离线文档
    innerHeight与clientHeight、innerWidth与clientWidth
    在Excel2010中输入身份证号
    JavaScript window.location对象
    Apache2.2+php5.2+the requested operation has failed
    Js中的window.parent ,window.top,window.self
    Mongoose 3.0 executable does not start
    Download ActionScript 3 reference files as a single zipActionScript 3 下载
    Flex 4.6 API 离线文档
  • 原文地址:https://www.cnblogs.com/yhdsir/p/4685237.html
Copyright © 2011-2022 走看看