public function audit_many1(){ $ids=I('post.data'); $ids_arr=explode(',',$ids); $i=0; foreach($ids_arr as $key=>$val){ $rechargeOb=D('Recharge'); $recharge=$rechargeOb->getSingleRecharge($val); if(empty($recharge['status'])){ ++$i; $payOb=D("PayLog"); $Model= M(); // 实例化一个空对象 $Model->startTrans(); // 开启事务 $data['amount']=$recharge['amount']+$recharge['amount_award']; $data['dealer_id']=$recharge['dealer_id']; $data['dealer_name']=$recharge['dealer_name']; $data['service_id']=$recharge['service_id']; $data['service_name']=$recharge['service_name']; $data['process_type']=$recharge['process_type']; $data['created_at']=date("Y-m-d H:i:s",time()); if(!empty($recharge['remark'])){ $data['remark']=$recharge['remark']; } if($i==2){ $re_i=false; }else{ $re_i=true; } $dealerOb=D('Dealer'); $dealer=$dealerOb->getSingleDealer($data['dealer_id']); $balance=$dealer['balance']; $recharge_status=C("recharge"); foreach($recharge_status as $k=>$v){ $recharge_data[]=$k; } if(in_array($data['process_type'],$recharge_data)){ $data['balance']=$balance+$data['amount']; $re_type=true; } $re_pay=$Model->table('yu_pay_log')->add($data); $data_dealer['balance']=$data['balance']; $re_dealer=$Model->table('yu_dealer')->where(array('id'=>$recharge['dealer_id']))->save($data_dealer); $re_recharge=$Model->table('yu_recharge')->where(array('id'=>$val))->save(array('status'=>1)); if($re_i && $re_type && $re_pay && $re_dealer && $re_recharge){ $Model->commit(); }else{ $Model->rollback(); } unset($data); } } $this->success('审核成功'); } public function audit_many(){ $ids=I('post.data'); $ids_arr=explode(',',$ids); $i=0; foreach($ids_arr as $key=>$val){ $rechargeOb=D('Recharge'); $recharge=$rechargeOb->getSingleRecharge($val); if(empty($recharge['status'])){ ++$i; $payOb=D("PayLog"); $payOb->startTrans(); // 开启事务 $data['amount']=$recharge['amount']+$recharge['amount_award']; $data['dealer_id']=$recharge['dealer_id']; $data['dealer_name']=$recharge['dealer_name']; $data['service_id']=$recharge['service_id']; $data['service_name']=$recharge['service_name']; $data['process_type']=$recharge['process_type']; $data['created_at']=date("Y-m-d H:i:s",time()); if(!empty($recharge['remark'])){ $data['remark']=$recharge['remark']; } $dealerOb=D('Dealer'); $dealer=$dealerOb->getSingleDealer($data['dealer_id']); $balance=$dealer['balance']; $recharge_status=C("recharge"); foreach($recharge_status as $k=>$v){ $recharge_data[]=$k; } if(in_array($data['process_type'],$recharge_data)){ $data['balance']=$balance+$data['amount']; }else{ $payOb->rollback(); continue; } $re_pay=$payOb->add($data); if(!$re_pay){ $payOb->rollback(); continue; } $data_dealer['balance']=$data['balance']; $re_dealer=$dealerOb->UpdateDealerbyId($recharge['dealer_id'],$data_dealer); if($re_dealer===FALSE){ $payOb->rollback(); continue; } $recharge_data1['status']=1; $re_recharge=$rechargeOb->UpdateRechargebyId($val,$recharge_data1); if($re_recharge===FALSE){ $payOb->rollback(); continue; } if($i==2){ $payOb->rollback(); continue; } $payOb->commit(); unset($data); } } $this->success('审核成功'); }
这是循环中每一项都使用一个事务的例子。
注意事务的写法,跳出循环还是比较优化的,补充一点,事务是针对数据库本身的,所以可以跨模型操作的 。注意事务的测试方法,也是值得借鉴的。