如何在同一笔交易中保存和删除 [英] How to save and delete in same transaction

查看:70
本文介绍了如何在同一笔交易中保存和删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要保存一些cms页面,并在一次交易中删除其他cms页面.

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

所以,我要怎么做:

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

单笔交易?作为参考,$ page1和$ page2都来自Mage :: getModel('cms/page').另外,我在此处找到了一个很好的答案,告诉我如何在事务中执行两次保存,但不执行保存和删除操作.怎么办?

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?

推荐答案

如果您必须在单个事务中执行此操作,只需在要删除的项目上调用isDeleted(true):

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

这篇关于如何在同一笔交易中保存和删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆