1

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 ?

0

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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.