您的位置:首页 > 编程语言 > PHP开发

thinkphp 多表事务处理

2018-02-06 10:10 363 查看
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;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: