总的来说,我是交易新手,尤其是 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。我的模型都在“狗”数据库上,所以我认为交易会进入模型的功能。也许你不能使用这样的模型。任何帮助将不胜感激!谢谢!