tp3.2 循环中使用事务

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('审核成功');
 }

这是循环中每一项都使用一个事务的例子。

注意事务的写法,跳出循环还是比较优化的,补充一点,事务是针对数据库本身的,所以可以跨模型操作的 。注意事务的测试方法,也是值得借鉴的。

转载于:https://my.oschina.net/u/3660147/blog/1592520

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值