Question

I’m currently using the following code to update/save invoices & orders after creating a partial invoice.

$invoice->getOrder()->setIsInProcess(true);
Mage::getModel('core/resource_transaction')
    ->addObject($invoice)
    ->addObject($invoice->getOrder())
;
$invoice->save();
$order->save();

To me, it looks like both $invoice->save() & the adding an object to the resource transaction do the same thing.

Should I still be using both, or should I only use one or the other?

Was it helpful?

Solution

You should use it like this:

$invoice->getOrder()->setIsInProcess(true);
Mage::getModel('core/resource_transaction')
    ->addObject($invoice)
    ->addObject($invoice->getOrder())
    ->save()
;

It is somehow similar with

$invoice->save();
$order->save();

The only difference is that both saves are done inside a single transaction. So if something goes wrong when saving the order, the invoice will be rolled back also. If you call them separately there is a chance that the invoice save will work and the order save won't. In this case only the order will be rolled back. This leads to inconsistent data in your db.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top