What's the point of disabling transactions? http://ellislab.com/codeigniter/user-guide/database/transactions.html
$this->db->trans_off()
I can't see the usage. Is it for testing purposes?
"When transactions are disabled, your queries will be auto-commited, just as they are when running queries without transactions."
I have a table called user
where there exists a column nameOfUser
. nameOfUser2
- column does NOT exist.
TEST1 This code would try to do 2 insertions with a normal transaction:
$this->db->trans_start();
$this->db->insert('user', array('nameOfUser' => 'testCVVCOOL'));
$this->db->insert('user', array('nameOfUser2' => 'test2'));
$this->db->trans_complete();
but it was rolled back (nothing is inserted) because the column nameOfUser2
-column does not exists in the second insert.
TEST2 This code would try to do 2 insertions with transaction disabled
$this->db->trans_off();
$this->db->trans_start();
$this->db->insert('user', array('nameOfUser' => 'testCVVCOOL'));
$this->db->insert('user', array('nameOfUser2' => 'test2'));
$this->db->trans_complete();
Above would insert testCVVCOOL string into user
-table even if there are an error in the second insert ($this->db->insert('user', array('nameOfUser2' => 'test2'));
)
When do you have the need for disabling transactions in this way?
When do you have the need for disabling transactions in this way?
My understanding is transactions don't allow more interaction in the same thread if there is an issue (as in your first example). So, if you wanted to do something else in the db before the commit and before the rollback, you wouldn't be able to.
Pseudo-example:
//will not work
$this->db->trans_start();
$this->db->insert('user', array('nameOfUser' => 'testCVVCOOL'));
$this->db->insert('user', array('nameOfUser2' => 'test2'));
//more likely this would be a condition that makes sense
//perhaps using a query result from earlier in function
if(1===1){
$this->db->query('INSERT INTO errors (db) values (1)');//nogo
}
$this->db->trans_complete();
-
//will work
$this->db->trans_off();
$this->db->trans_start();
$this->db->insert('user', array('nameOfUser' => 'testCVVCOOL'));
$this->db->insert('user', array('nameOfUser2' => 'test2'));
//more likely this would be a condition that makes sense
//perhaps using a query result from earlier in function
if(1===1){
$this->db->query('INSERT INTO errors (db) values (1)');//OK
}
$this->db->trans_complete();
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