Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Mage::getModel('core/resource_transaction') and when to use this model in Magento?

In Magento's core code, Mage::getModel('core/resource_transaction') , what does this model do and in what situation do we want to initiate and use this core model class? Does this model has a corresponding table in the Magento database?

like image 306
s-hunter Avatar asked Dec 09 '22 10:12

s-hunter


2 Answers

This is a resource Model class (Mage_Core_Model_Resource_Transaction).

In example:

When you want to change something in a product and the same time you want to change something in a category you are going to instantiate catalog/category and catalog/product. And make your changes like this:

$prod_id = 554;
$cat_id = 323;

$product_object = Mage::getModel('catalog/product')->load($prod_id);
$product_object->setName('NewProductName');

try{
    $product_object->save();
    echo "Transaction for PRODUCT completed successfully";
} catch(EXCEPTION $e) {
    echo $e->getMessage();
}

$category_object = Mage::getModel('catalog/category')->load($cat_id);
$category_object->setName('NewCatName');

try{
    $category_object->save();
    echo "Transaction for Category completed successfully";
} catch(EXCEPTION $e) {
    echo $e->getMessage();
}

As you can see in the code above we save() 2 times. One for product and one for category. Catching 2 different exceptions, and 2 different transactions to the database.

In some (rare i think) cases is required to make changes using 2 or more objects and save to the database all in a Single Transaction.

So in our case we add the $product_object and $category_object to a Mage::getModel('core/resource_transaction') instance:

$one_transaction = Mage::getModel('core/resource_transaction');
$one_transaction->addObject($product_object);
$one_transaction->addObject($category_object);
try{
    $one_transaction->save();
} catch(EXCEPTION $e) {
    echo $e->getMessage();
}

The profit in this case is that if something goes wrong with one or more objects we added, no changes are posted to the database.

like image 105
Nikitas Avatar answered May 23 '23 12:05

Nikitas


No, this model does not use any tables. It is needed to make MySQL transactions functionality. For example, you want to save invoice, shipment and order, but if something goes wrong, to rollback all previously saved data. You can do so:

$transactionSave = Mage::getModel('core/resource_transaction')
    ->addObject($invoice)
    ->addObject($order)
    ->addObject($shipment);
$transactionSave->save();

Transaction save method will go through all objects and call save method in them. If something will go wrong during shipment save, for example, Magento will call $this->_rollbackTransaction(); method (class Mage_Core_Model_Resource_Transaction, line 166) and will rollback all previously saved data using MySQL functionality.

If you want to call another then save method for your object, you may use addCommitCallback method

$transaction->addCommitCallback(array($order, 'place'));

Then after calling $transactionSave->save(); it will call $order->place() method. For more examples go to class Mage_Sales_Model_Service_Quote class - it uses transactional save in lots of places.

like image 27
freento Avatar answered May 23 '23 10:05

freento