IN Magento如何在单个事务中的多个表中插入数据,如果进程中有任何错误,则回滚。我可以编写自定义查询并使用事务,但我更愿意,如果我可以使用Magento方法。
Magento Database Transaction 操作代码如下:
$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();
}
下面是magento事务的案例
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
try {
$connection->beginTransaction();
$model = Mage::getModel('sales/order_creditmemo')->load($id);
$model->setNewOrderId($incrementId);
$model->setStoreCreditUsed($store_credit_used);
$query = $model->save();
$historyModel = Mage::getModel('amstcred/balanceHistory')->load($historyId);
$historyModel->setIsRelationOrder(1)->save();
$connection->commit();
} catch (Exception $e) {
$connection->rollback();
}
本文参考:
https://stackoverflow.com/questions/7283010/magento-database-transaction