CodeIgniter Forums
Transactions in Codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Transactions in Codeigniter (/showthread.php?tid=50847)



Transactions in Codeigniter - El Forum - 04-10-2012

[eluser]bleu[/eluser]
Can I use transaction statements in controller as I need to run one query then another one which will retrieve data use that retrieved data to insert with another query.

I need all this in transactions.

How can I achieve that.

Will something like the below example work in model or do I need to use the combination of controller and model.


Code:
$this->db->trans_begin();

$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY to retrieve data...');

run data in loop create variables and insert in below query

$this->db->query('AND YET ANOTHER QUERY...');

if ($this->db->trans_status() === FALSE)
{
    $this->db->trans_rollback();
}
else
{
    $this->db->trans_commit();
}



Transactions in Codeigniter - El Forum - 04-11-2012

[eluser]smilie[/eluser]
In short - yes, you can do that.

What I have learned (by accident, but never the less) is that all queries between start and end of transaction are dealt by DB transaction system. It does not matter _where_ queries are, for as long as they are inside transaction start / end.

This means, that you can start transaction in controller, then execute queries in models, helpers, libraries - where ever - for as long as they are called inside start / end of transaction.

Have fun!

Cheers,
Smilie