问题
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