問題描述
我正在研究 Magento 模塊,需要知道是否可以回滾一系列模型保存.基本上,我有五個模型加上我模塊中的幾個模型,我需要一個接一個地保存:
I'm working on a Magento module and need to know if it's possible to roll back a series of model saves. Basically, I have five models plus several from my module that I need to save one after the other:
admin/role
admin/user
core/website
core/store_group
core/store
mymodule/model1
mymodule/model2
我的問題是,每當(dāng)這些模型中的任何一個拋出異常時,我都需要進(jìn)入 MySQL 并手動刪除所有已保存的行.這是非常沒有成效的.
My problem is that whenever any of these models throw an exception, I need to go into MySQL and manually delete all the rows that were saved. This is very unproductive.
我很確定 Magento 沒有我可以在我的上下文中訪問的回滾過程.比如我查看了Mage_Core_Model_Abstract
,在save方法中,回滾機(jī)制都是受保護(hù)的.
I'm pretty sure that Magento doesn't have a rollback procedure that I can access in my context. For example, I looked in Mage_Core_Model_Abstract
and in the save method, the rollback mechanisms are all protected.
那么,我的問題是,在 Magento 中進(jìn)行數(shù)據(jù)庫事務(wù)是否有一些我應(yīng)該注意的最佳實(shí)踐?
So, my question is, is there some best practice for doing database transactions in Magento that I should be aware of?
推薦答案
我在核心代碼中看到了以下內(nèi)容,看起來正是您訂購的.
I've seen the following used in core code, and it looks like its just what you ordered.
$transactionSave = Mage::getModel('core/resource_transaction');
$transactionSave->addObject($model_one)
$transactionSave->addObject($model_two)
$transactionSave->save();
core/resource_transaction
對象允許您添加 Magento 對象,并對它們執(zhí)行批量保存.試一試,我很想在評論中聽到這對你有用或沒用.
The core/resource_transaction
object allows you to add Magento objects, and perform a mass save on them. Give that a try, and I'd love to hear how this does, or doesn't, work for you in the comments.
這篇關(guān)于Magento 捕獲異常并回滾數(shù)據(jù)庫事務(wù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!