php - Magento multiple database transactions in single commit -
i know how in zend framework
$db->begintransaction(); try { $db->query(...); $db->query(...); $db->query(...); . . . $db->commit(); } catch (exception $e) { $db->rollback(); }
but want using magento model, like
$db->begintransaction(); try { $modelone = mage::getmodel('modulename/table1'); $modeltwo = mage::getmodel('modulename/table2'); $modelthree = mage::getmodel('modulename/table3'); $db->query($modelone); $db->query($modeltwo); $db->query($modelthree); . . . $db->commit(); } catch (exception $e) { $db->rollback(); }
if of them failed save should rolled back
thanks
look @ app/code/core/mage/core/model/resource/transaction.php
this model allow add models objects in transaction. during save call $object->save()
each added object. if fails, call $object->getresource()->rollback()
each object. can add commit callbacks via addcommitcallback(array($object, 'callbackfunctionname'))
.
if need delete transaction, call $transaction->delete()
instead of $transaction->save()
in case call $object->delete()
instead of $object->save()
each object.
example:
try { $transaction = mage::getmodel('core/resource_transaction') ->addobject(mage::getmodel('modulename/table1')) ->addobject(mage::getmodel('modulename/table2')) ->addobject(mage::getmodel('modulename/table3')); $transaction->save(); } catch (exception $e) { echo $e->getmessage(); }
Comments
Post a Comment