3

When you save your multiple Model Object object then I want to provide Transaction. How can I achieve in magento?

Any one Please Guide Me?

1
  • what do you mean by multiple Model Object?
    – Marius
    Commented Feb 11, 2014 at 14:49

2 Answers 2

11

I hope I understood correctly your question.
If you want to save X objects at once and roll back all of them in case something goes wrong try this.

$transaction = Mage::getModel('core/resource_transaction');
$transaction->addObject($obj1);
$transaction->addObject($obj2);
...
$transaction->addObject($objX);

$transaction->save();

This will call ->save() for each object you added using addObject but everything will be done inside a transaction.

1
  • Hi @Marius, I want to get id of $obj1 before addObject $obj2. How to do this ?
    – MrTo-Kane
    Commented May 26, 2016 at 1:47
3

Have a look e.g. into \Mage_Eav_Model_Entity_Abstract::saveAttribute

public function saveAttribute(Varien_Object $object, $attributeCode)
{
    // ...
    $adapter        = $this->_getWriteAdapter();
    // ...
    $adapter->beginTransaction();

    try {
        // ...
        $adapter->commit();
    } catch (Exception $e) {
        $adapter->rollback();
        throw $e;
    }

    return $this;
}
2
  • I have 3 Model Object like product, order and custom table then this will work?
    – Keyul Shah
    Commented Feb 11, 2014 at 14:20
  • mysql transactions work for select/update/delete. As long as you don't create tables, this work. You should start the transaction before the first save and commit it after the last save - somehow. Commented Feb 12, 2014 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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