Magento捕获异常并回滚数据库事务 [英] Magento catching exceptions and rolling back database transactions

查看:206
本文介绍了Magento捕获异常并回滚数据库事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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

我的问题是,每当这些模型抛出异常,我需要进入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 ,在保存方法中,回滚机制都受到保护。

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做数据库事务,我应该知道?

So, my question is, is there some best practice for doing database transactions in Magento that I should be aware of?

推荐答案

在核心代码中使用以下代码,它看起来像你所订购的。

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对象,并对它们执行大量保存。试试看,我很想听听这是怎么做的,不喜欢在评论中为你工作。

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.

这篇关于Magento捕获异常并回滚数据库事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆