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     }

 

 


 

 
posted @ 2017-06-26 17:04  wahaha2000  阅读(2837)  评论(0编辑  收藏  举报