
正文
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;
}







