How to save and delete in same transaction

magento, php, transactions

Solution

If you must do this in a single transaction, just call `isDeleted(true)` on those items which you wish to be deleted:

//Build out previous items, then for each which should be deleted...
$page2->isDeleted(true);

$transaction = Mage::getModel('core/resource_transaction');
$transaction->addObject($page1)
$transaction->addObject($page2)
//$transaction->addObject(...) etc...
$transaction->save();

Thought I should add an explanation (from `Mage_Core_Model_Abstract::save()` [link]):

/**
 * Save object data
 *
 * @return Mage_Core_Model_Abstract
 */
public function save()
{
    /**
     * Direct deleted items to delete method
     */
    if ($this->isDeleted()) {
        return $this->delete();
    }
    // ...
}

Problem

I need to save some cms pages and delete others in a single transaction. So, how to I make this: ``` $page1->save(); $page2->delete(); ``` A single transaction? For reference, both $page1 and $page2 come from Mage::getModel('cms/page'). Also, I found an excellent answer here that tells me how to do two saves in a transaction, but not how to do both a save and delete. How can it be done?

Original source

Related problems