问题描述
是否可以在交易提交后回滚交易?
我问这是因为在Datamapper文档中我看到 trans_begin()
方法,但我没有找到 trans_end / code>方法。 Codeigniter有
trans_complete()
方法,所以我假设Datamapper可能有类似的方法。
Is it possible to rollback a transaction after it has been commited?I ask this because in the Datamapper documentation i see the trans_begin()
method, but i didn't find the trans_end()
method. Codeigniter has the trans_complete()
method, so i assumed Datamapper might have a similar method.
有趣的是。是否有类似于Datamapper / Codeigniter中保存点的内容?
One thing I found interesting is this answer. Is there anything similar to a savepoint in Datamapper/Codeigniter?
推荐答案
DataMapper处理事务的方式与
CodeIgniter所做的一样(读取CodeIgniter事务),显然是因为
使用相同的方法!唯一的真正的区别是,你将
直接调用你的DataMapper对象上的事务方法。
DataMapper handles transactions in very much the same way that CodeIgniter does (read CodeIgniter Transactions), obviously because it uses the same methods! The only real difference is that you'll be calling the transaction methods directly on your DataMapper objects.
而不是:
$this->db->trans_begin();
您可以使用:
$my_datamapper_object->trans_begin();
根据Datamapper文档,其他所有内容与Codeigniter 完全相同关于交易。如果你看看Datamapper库的源代码,你会看到所有 trans _ *()
调用都是简单的封装函数。示例:
Everything else, according to the Datamapper documentation, is identical to Codeigniter in regards to transactions. If you look at the source code for the Datamapper library, you'll see that all trans_*()
calls are simply wrapper functions. Example:
// Datamapper.php 1.8.dev line 3975
/**
* Trans Complete
*
* Complete a transaction.
*
* @return bool Success or Failure
*/
public function trans_complete()
{
return $this->db->trans_complete();
}
A trans_end()
方法不存在于Codeigniter或Datamapper中。对于自动事务,您可以使用 trans_start()
和 trans_complete()
,或手动调用它们:
A trans_end()
method does not exist in either Codeigniter or Datamapper. You'd use trans_start()
and trans_complete()
for automatic transactions, or to call them manually:
如果您要手动运行事务,您可以这样做,因为
如下:
If you would like to run transactions manually you can do so as follows:
$this->db->trans_begin();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
}
else
{
$this->db->trans_commit();
}
$ this-> db 与您的Datamapper对象。例如
Just replace $this->db
with your Datamapper object. For example.
$u = new User($id);
$u->trans_begin();
$u->name = 'Jorge';
$u->save();
if ($u->trans_status() === FALSE)
{
$u->trans_rollback();
}
else
{
$u->trans_commit();
}
这篇关于在使用Datamapper / Codeigniter提交事务后,我可以回滚事务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!