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.
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
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With