[英]Codeigniter transactions with other non-db functions?
is it possible to have a code like this? 是否可以有这样的代码?
// send email with email+password and create user (in one transaction)
$this->db->trans_start();
$this->User_model->add_user($user_object);
MY_Controller::send_mail($to, $subject, $mail_data);
$this->db->trans_complete();
It's codeigniter. 它是代码点火器。 I use active record class.
我使用活动记录类。 Static function send_mail returns FALSE if failed, add_user returns FALSE if affected rows == 0...
静态函数send_mail如果失败则返回FALSE,如果受影响的行== 0,则add_user返回FALSE ...
I would like to have that non-db function (send_mail) in transaction and if it fails - do rollback. 我想在事务中使用该非数据库功能(send_mail),如果失败,请回滚。
I know there is something like $this->db->trans_status()
and I can run transactions manualy, but I think about this much more nicer code block. 我知道这里有
$this->db->trans_status()
,我可以$this->db->trans_status()
运行事务,但是我认为这是一个更好的代码块。 Is it even possible to have non-db functions in transaction this way? 这样甚至可以在事务中具有非数据库功能吗?
No, you can only do a manual rollback in case that send_mail()
fails: 不,您只能在
send_mail()
失败的情况下进行手动回滚:
if ( ! MY_Controller::send_mail($to, $subject, $mail_data))
{
$this->db->trans_rollback();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.