zoukankan      html  css  js  c++  java
  • cake使用事务的方法

    $db = $this->UserAccount->getDataSource();
                $db->begin();
    
                $this->UserAccount->updateAll(array('amount' => 'UserAccount.amount - ' . $total_price), array('user_id' => $user_id, 'amount >=' => $total_price));
                $affectedrows = $this->UserAccount->getAffectedRows();
                if (!$affectedrows) { // 没有响应行数则为扣失败
                    $this->log("VoucherComponent->send UserAccount 他帐号( $user_id )上没钱 $user_id = $user_id, \$total_price = $total_price");
                    $db->rollback();
                }
    
                $data['MerchantProductOrder']['voucher_number'] = $this->Voucher->generateQrCode();
                if ($data['MerchantProductOrder']['voucher_total'] < 1) {
                    $this->error('错误的凭证总数');
                    $db->rollback();
                }
    
                $data['MerchantProductOrder']['voucher_remaining'] = $data['MerchantProductOrder']['voucher_total'];
                $data['MerchantProductOrder']['send_type']='sms+mms';
                $data['MerchantProductOrder']['sms_content']='';
                $data['MerchantProductOrder']['mms_content']=''; 
                $data['MerchantProductOrder']['mms_title']='';
                $data['MerchantProductOrder']['callback_url']='';
                $data['MerchantProductOrder']['buyer_user_id']=$user_id;
    
                $ret = $this->MerchantProductOrder->save($data);
                $send_result = -1;
                if ($ret) {
                    $send_result = $this->Voucher->send(
                        $ret['MerchantProductOrder']['id'], $data['MerchantProductOrder']['voucher_number'], $data['MerchantProductOrder']['mobile'], $data['MerchantProductOrder']['send_type'], $data['MerchantProductOrder']['sms_content'], $data['MerchantProductOrder']['mms_content'], $data['MerchantProductOrder']['mms_title']
                    );
                }
    
                if ($send_result == 0) {
                    $db->commit();
                    $this->succ('订单已经成功添加,订单ID:' . $ret['MerchantProductOrder']['id']);
                    $this->redirect(array('action' => 'buy_succ', $ret['MerchantProductOrder']['id']));
                }else{
                    $db->rollback();
                    $this->log("ecommerceet_buy 创建订单失败 ");
                    $this->error('创建订单失败');
                }

    方法

    1、获取数据库资源

    2、开始事务

    3、如果有问题就回滚

    4、如果没有问题就提交

  • 相关阅读:
    Day1:T1 模拟 T2 拓扑排序
    vijos1060 隔板法
    字符串处理:kmp算法
    vijos1004 博弈论
    vijos1009:扩展欧几里得算法
    有关浮点数的精度问题
    C++构造 下一个排列 的函数
    vijos1053 用spfa判断是否存在负环
    SPFA和FLOYD算法如何打印路径
    细节MARK
  • 原文地址:https://www.cnblogs.com/linksgo2011/p/3031053.html
Copyright © 2011-2022 走看看