zoukankan      html  css  js  c++  java
  • TP 多表事务处理, 异常处理

                    $tran_result = true;
                    $trans = M();
                    $trans->startTrans();   // 开启事务
    
                    try {   // 异常处理
                        // 更新实施
                        $busbidList = M("busbid")->where($map)->select();
                        foreach($busbidList as $k => $v) {
                            $map['id'] = $busbidList[$k]['id'];
                            $result = M('busbid')->where($map)->data($data)->save();
                            if ($result === false) {
                                throw new Exception(“错误原因”);
                            }
                        }
                    } catch (Exception $ex) {
                        $tran_result = false;
                        // 记录日志
                        Log::record("== xxx更新失败 ==", 'DEBUG'); 
                        Log::record($ex->getMessage(), 'DEBUG');
                    }
    
                    if ($tran_result === false) {
                        $trans->rollback();
                        // 更新失败
                        $array['status'] = 0;
                    } else {
                        $trans->commit();
                        // 更新成功
                        $array['status'] = 1;
                    }
             


     1 /*
     2 
     3      * 添加酒店和房型
     4 
     5      * */
     6 
     7     public function insertAll($arr_hotel=array(),$arr_room=array()){
     8 
     9 
    10         $model = new Model();
    11 
    12         $model->startTrans();
    13 
    14 
    15         $flag=false;
    16 
    17 
    18         $hid = $model->table(C('DB_PREFIX').'hotel')->add($arr_hotel);
    19 
    20         if( $hid && count($arr_room) ==0 ){//如果没有传入房型的信息则,直接提交数据
    21 
    22           
    23 
    24             $flag=true;
    25 
    26         }else if( $hid && count($arr_room) >= 0){//存在对应房型信息,则添加对应的酒店编号,并处理提交
    27 
    28             for($i=0 ; $i<count($arr_room) ; $i++){
    29 
    30                 $arr_room[$i]['hid'] = $hid;
    31 
    32             }
    33 
    34             $rid = $model->table(C('DB_PREFIX').'room')->addAll($arr_room);
    35 
    36             if( $rid ){
    37 
    38                 $model->commit();
    39 
    40                 $flag=true;
    41 
    42             }
    43 
    44         }
    45 
    46 
    47         if(!$flag){ 
    48 
    49                $model->rollback();
    50 
    51         }else{
    52 
    53              $model->commit();
    54 
    55        }
    56 
    57         return $flag;
    58 
    59     }
    
    
    
     
    wahaha每天学习一点点
  • 相关阅读:
    Eclipse中支持js提示
    数据库命名规则
    JavaWeb 命名规则
    Ajax&json
    js中,var 修饰变量名和不修饰的区别
    javaScript知识点
    Bootstrap 栅格系统
    文本框如果不输入任何内容提交过后是一个空字符串还是null
    根据汇总数量依次扣减的SQL新语法
    asp.net中使用forms验证
  • 原文地址:https://www.cnblogs.com/liuzhaobo1999/p/7081398.html
Copyright © 2011-2022 走看看