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('審覈成功');
 }

這是循環中每一項都使用一個事務的例子。數據庫

注意事務的寫法,跳出循環仍是比較優化的,補充一點,事務是針對數據庫自己的,因此能夠跨模型操做的 。注意事務的測試方法,也是值得借鑑的。post

相關文章
相關標籤/搜索