thinkphp 多表事务处理

try{
    $this->user = D('User');
    $this->user->startTrans(); //开始事务
	$res = $this->user->Saveuser($where,$data);
	$res1 = $this->user->Saveuser($where,$data);
	if(!$res||!$res1){
		throw new \Exception('数据实例化失败'); 
	}
	$this->user->commit(); //确认事务
}catch(\Exception $e){
	$this->user->rollback(); //回滚

}

 

    /*
     * 添加酒店和房型
     * */
    public function insertAll($arr_hotel=array(),$arr_room=array()){

        $model = new Model();
        $model->startTrans();

        $flag=false;

        $hid = $model->table(C('DB_PREFIX').'hotel')->add($arr_hotel);
        if( $hid && count($arr_room) ==0 ){//如果没有传入房型的信息则,直接提交数据
          
            $flag=true;
        }else if( $hid && count($arr_room) >= 0){//存在对应房型信息,则添加对应的酒店编号,并处理提交
            for($i=0 ; $i<count($arr_room) ; $i++){
                $arr_room[$i]['hid'] = $hid;
            }
            $rid = $model->table(C('DB_PREFIX').'room')->addAll($arr_room);
            if( $rid ){
                $model->commit();
                $flag=true;
            }
        }

        if(!$flag){ 
               $model->rollback();
        }else{
             $model->commit();
       }
        return $flag;
    }

  

转载于:https://www.cnblogs.com/microtiger/p/8420998.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值