Welcome Guest, Not a member yet? Register   Sign In
Transactions in Codeigniter
#1

[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();
}
#2

[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




Theme © iAndrew 2016 - Forum software by © MyBB