首选innodb存储引擎才支持事务。
$table=M('table');
$table->startTrans();//开启事务
$table->commit();//成功则提交
$table->rollback();//不成功,则回滚
详见代码:
$returnorder=M('return_order_goods_info');
$returnorder->startTrans();//开启事务
$data['type']=I('tmp_type');
$data['description']=I('description');
$data['return_type']=I('tem_return_type');
$data['pick_up_province']=I('rev_province');
$data['pick_up_city']=I('rev_city');
$data['pick_up_county']=I('rev_county');
$data['pick_up_address']=I('rev_address');
$data['create_time']=time();
$orderinfo['sku_name']=I['sku_name'];
$orderinfo['sku_default_image']=I['sku_default_image'];
$resultinfo=M('return_order_commodity_info')->add($orderinfo);
$ceshi=M("return_order_commodity_info")->getLastSql();
if($result && $resultinfo){
$returnorder->commit();//成功则提交
$this->ajaxReturn(array("data"=>$data,"info"=>'已提交成功,请耐心等待工作人员审核',"status"=>1));
}else{
$returnorder->rollback();//不成功,则回滚
$this->ajaxReturn(array("data"=>$data,"info"=>'未提交成功,请联系客服',"status"=>0));
}
第二种:
$trans_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) {
$trans_result = false;
// 记录日志
Log::record("== xxx更新失败 ==", 'DEBUG');
Log::record($ex->getMessage(), 'DEBUG');
}
if ($trans_result === false) {
$trans->rollback();
// 更新失败
$array['status'] = 0;
} else {
$trans->commit();
// 更新成功
$array['status'] = 1;
}