I want to load a model and for update equivalent sql :
START TRANSACTION;
SELECT * FROM table WHERE id = 'id' FOR UPDATE;
COMMIT;
May magento model have the same method ?
You can try Mage::getModel('core/resource_transaction')
. The documentation for it such as it is here.
OR
Look at app/code/core/Mage/Core/Model/Resource/Transaction.php
This model allow to add another Models as objects in transaction. During save it call $object->save()
for each added object.
If something fails, it call $object->getResource()->rollBack()
for each object.
You also can add commit callbacks via addCommitCallback(array($object, 'callbackFunctionName'))
.
If you need delete transaction
, call $transaction->delete()
instead of $transaction->save()
In this case it call $object->delete()
instead of $object->save()
for 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();
}