Magento Database Transaction

≡放荡痞女 提交于 2019-12-21 01:16:33

问题


IN Magento How can I insert data in multiple tables in a single transaction and rollback if there is any error in the process.?? I can write custom queries and use transactions but I would prefer if I can do it using Magento methods.


回答1:


You can try Mage::getModel('core/resource_transaction'). The documentation for it such as it is here.

But probably more useful, here is an example of using it to create an invoice from an order.




回答2:


The accepted answer is fine if what you are attempting to do is model saves. This will let you chain any number together with rollback.

If, however, you are performing other actions that might trigger roll-back or are rolling back themselves, then you want to use something more low-level:

$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
try {
    $connection->beginTransaction();

    // Make saves and other actions that affect the database

    $connection->commit();
} catch (Exception $e) {
    $connection->rollback();
}

You can also get the connection from a model, but there may not be one available.



来源:https://stackoverflow.com/questions/7283010/magento-database-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!