问题描述
可以有一个事务与单独的模型。
我要插入带有标签的帖子。标签和帖子是在两个单独的模型。
我如何能够处理它与一个事务? (如下:)
Is it possible to have one "transaction" with to separate models.I want to insert a post with their tags. Tags and Posts are in two separate models.How can i achive to handle it with a transaction? (Like below:)
$this->db->trans_start();
$this->post_model->insert('...');
$this->tags_model->insert('...');
$this->db->trans_complete();
推荐答案
只要没有其他事务语句在你的模型方法中,你的示例代码应该很好。
As long as you don't have other transaction statements in your model methods, your sample code should work fine.
根据文档,你可以通过传递 TRUE
到 $ this-> db-> trans_start()
:
As per the documentation, you can test it by passing TRUE
to $this->db->trans_start()
:
$this->db->trans_start(TRUE);
// Queries/model calls
$this->db->trans_complete();
if($this->db->trans_status() === FALSE)
{
// do something if it fails
}
传递 TRUE
至 trans_start ()
将在完成后自动回滚事务。您应该可以检查表上的auto_increment值(如果适用),以查看事务是否有效。
Passing TRUE
to trans_start()
will automatically rollback the transaction upon completion. You should be able to check auto_increment values on your tables (if applicable) to see if the transaction worked or not.
这篇关于Codeigniter一个事务用两个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!