使用beginTransaction();和commit();在magento的管理员控制器中

时间:2015-07-30 04:46:21

标签: mysql magento model transactions controller

我创建了一个新的群发行动,执行批量发票操作和批量发货。它工作正常。

  

但问题是当我们从2个浏览器执行相同的操作时   同一订单的同一时间,它为同一订单创建多个发票   订单

下面是我的代码

import UIKit
import AVFoundation

class VesuviusViewController: UIViewController {


@IBOutlet weak var PausePlay: UIButton!



@IBAction func playPressed(sender: AnyObject) {
    VesSet()
}
var audioPlayer: AVAudioPlayer!

    func VesSet(){


        do {
            self.audioPlayer =  try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ves", ofType: "m4a")!))
            self.audioPlayer.play()

        } catch {
            print("Error")
        }


    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

    }
}

我想通过使用

来克服这个问题
        foreach ($orderIds as $orderIdss) {
$this->beginTransaction();
            $order       = Mage::getModel('sales/order')->load($orderIdss); // to load order details
            $ordrestatus = $order->getStatus();

            try {
                if (!$order->canInvoice()) // to check if the order can be invoiced or not
                    {
                    $error_id[] = $order->getIncrementId();

                    $error++;
                } else if ($ordrestatus !== 'pending') { // to restrict order not to be invoiced if the status is not "pending"
                    $error_id[] = $order->getIncrementId();
                    $error++;
                } else if ($ordrestatus == 'pending') {
                    $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
                    if (!$invoice->getTotalQty()) {
                        Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
                    }
                    $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
                    $invoice->register();
                    $transactionSave = Mage::getModel('core/resource_transaction')->addObject($invoice)->addObject($invoice->getOrder());
                    $transactionSave->save();
                    $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save(); // to change order state to processing
                    $success_ids[] = $order->getIncrementId();
                    $success++;
                }
            }
            catch (Mage_Core_Exception $e) {
                $error_id[] = $order->getIncrementId();
                $error++;
            }
        }

但它会引发以下错误

 beginTransaction(); and commit();

你能告诉我如何使用资源模型和beginTransaction();和commit();在管理员控制器中。

感谢

1 个答案:

答案 0 :(得分:2)

beginTransaction()commit()是类Mage_Core_Model_Resource_Abstract中的函数。

您正在使用$this上的功能,在此上下文中控制器Iclp_Batchupdate_Adminhtml_BatchupdateController

您应该在资源模型上使用这些功能。

例如,您的变量$order模型,您可以获取资源,因此以这种方式调用beginTransation()$order->_getResource()->beginTransaction();

<强>加成:

您可能希望通过回滚来管理例外

$resource_model->beginTransaction();
try {
    //do your stuf...
    $resource_model->commit();
} catch (Exception $e) {
    $resource_model->rollBack();
    throw $e;
}