How to save and delete in one transaction

I need to save some cms pages and delete others in one transaction.

So how to do it:

$page1->save(); $page2->delete(); 

One transaction? For reference, both $ page1 and $ page2 come from Mage :: getModel ('cms / page'). Also, I found a great answer here , which tells how to make two transactions in a transaction, but not how to make and save and delete. How can I do that?

+3
source share
1 answer

If you must do this in one transaction, just call isDeleted(true) for those elements that you want to remove:

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

I think 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(); } // ... } 
+10
source

Source: https://habr.com/ru/post/1209224/


All Articles