How to save and delete in same transaction

萝らか妹 提交于 2020-01-01 16:38:29

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/13478840/how-to-save-and-delete-in-same-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!