Codeigniter 中具有多个表的事务
总的来说,我对事务很陌生,尤其是 CodeIgniter。我正在使用 InnoDB 和其他所有东西,但我的事务在我想要的时候并没有回滚。这是我的代码(稍微简化)。
$dog_db = $this->load->database('dog', true);
$dog_db->trans_begin();
$dog_id = $this->dogs->insert($new_dog); //Gets primary key of insert
if(!$dog_id)
{
$dog_db->trans_rollback();
throw new Exception('We have had an error trying to add this dog. Please go back and try again.');
}
$new_review['dog_id'] = $dog_id;
$new_review['user_id'] = $user_id;
$new_review['date_added'] = time();
if(!$this->reviews->insert($new_review)) //If the insert fails
{
$dog_db->trans_rollback();
throw new Exception('We have had an error trying to add this dog. Please go back and try again.');
}
//ADD DESCRIPTION
$new_description['description'] = $add_dog['description'];
$new_description['dog_id'] = $dog_id;
$new_description['user_id'] = $user_id;
$new_description['date_added'] = time();
if(!$this->descriptions->insert($new_description))
{
$dog_db->trans_rollback();
throw new Exception('We have had an error trying to add this dog. Please go back and try again.');
}
$dog_db->trans_rollback(); //THIS IS JUST TO SEE IF IT WORKS
throw new Exception('We have had an error trying to add this dog. Please go back and try again.');
$dog_db->trans_commit();
}
catch(Exception $e)
{
echo $e->getMessage();
}
我没有收到任何错误消息,但也没有回滚。它应该在提交之前的最后一个 trans_rollback 处回滚。我的模型都在“狗”数据库上,所以我认为事务将带入模型的功能中。也许你只是不能使用这样的模型。任何帮助将不胜感激!谢谢!
I'm new to transactions in general, but especially with CodeIgniter. I'm using InnoDB and everything, but my transactions aren't rolling back when I want them to. Here's my code (slightly simplified).
$dog_db = $this->load->database('dog', true);
$dog_db->trans_begin();
$dog_id = $this->dogs->insert($new_dog); //Gets primary key of insert
if(!$dog_id)
{
$dog_db->trans_rollback();
throw new Exception('We have had an error trying to add this dog. Please go back and try again.');
}
$new_review['dog_id'] = $dog_id;
$new_review['user_id'] = $user_id;
$new_review['date_added'] = time();
if(!$this->reviews->insert($new_review)) //If the insert fails
{
$dog_db->trans_rollback();
throw new Exception('We have had an error trying to add this dog. Please go back and try again.');
}
//ADD DESCRIPTION
$new_description['description'] = $add_dog['description'];
$new_description['dog_id'] = $dog_id;
$new_description['user_id'] = $user_id;
$new_description['date_added'] = time();
if(!$this->descriptions->insert($new_description))
{
$dog_db->trans_rollback();
throw new Exception('We have had an error trying to add this dog. Please go back and try again.');
}
$dog_db->trans_rollback(); //THIS IS JUST TO SEE IF IT WORKS
throw new Exception('We have had an error trying to add this dog. Please go back and try again.');
$dog_db->trans_commit();
}
catch(Exception $e)
{
echo $e->getMessage();
}
I'm not getting any error messages, but it's not rolling back either. It should roll back at that final trans_rollback right before the commit. My models are all on the "dog" database, so I think that the transaction would carry into the models' functions. Maybe you just can't use models like this. Any help would be greatly appreciated! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我知道这篇文章已经过时了,但这是我的 2 美分:
我不认为这个:
会起作用,因为 CI 活动记录的插入函数总是返回 TRUE(成功与否)。如果您使用调试模式,CI 将在错误时停止并向用户抛出一条屏幕消息,但插入函数仍将返回 TRUE。
因此,如果您愿意使用 CI“手动”控制事务,则必须使用如下内容:
希望这可以帮助某人...有时...某地
Well, I know this post is antique, but here's my 2 cents:
I don't think this:
will work, cause the insert function from CI active record always returns TRUE (succeeding or not). If you are using debug mode, CI will stop on error and throw a screen message to the user, but insert function will still returns TRUE.
So, if you're willing to control transactions "manualy" with CI, you'll have to use something like this:
Hope this helps someone...sometime....somewhere
也许,这是因为您使用 $dog_db 连接,并回滚不存在的 $booze_db 事务?(或者这是一个拼写错误?)
Maybe, it's because you connected using $dog_db, and rolling back nonexisting $booze_db transaction ?(or it's a typo?)