DB Transaction with CodeIgniter

DB Transaction with CodeIgniter

Posted by Rukmi Patel | August 13, 2016
db-transaction-codeigniter

Transaction in CodeIgniter

CodeIgniter’s database abstraction allows you to use transactions with databases that support transaction-safe table types. In MySql, InnoDB table type provides support for transaction.
CodeIgniter facilitates developers so that either CI manages commit and rollback commands or allows its developers to do it.

DB Transaction Managed By CI

$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');
$this->db->trans_complete();

You can write as many queries as you want between trans_start() and trans_complete(). System will commit or rollback based to success of failure of all queries.

You can pass value as “true” in-order to check rollback functionality. If you pass “true” as value in trans_start() function, it will not add values in DB even if System executes insert DB command.

$this->db->trans_start(true);

Transactions Managed by Developers Manually

$this->db->trans_begin();

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

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

By using CodeIgniter’s default functions or writing code manually, Developer can provide support for database transactions and ensure Data Integrity.

Blog Comments

dude, i have MS SQL database, i have to read the docs, and do yours, but the error, are this trans support on MS SQL, ora just on mysql?

Hey Roflik,

Are you using DBAL provided by CodeIgniter or writing raw queries? I am sure CI support MS SQL if you take DBAL approach.

Cheers,
Rukmi.

Add a comment

*Please complete all fields correctly

Related Blogs

Posted by Rukmi Patel | December 2, 2019
iTreeni Technolabs Outshines at GoodFirms by Providing End-To-End Business Solutions
Empowering clients' business by leveraging technology-enabled solutions endows iTreeni Technolabs to tap into the list of top web development companies in Australia at GoodFirms. View iTreeni Technolabs' GoodFirms' profile to...
MEAN-Vs-Full-stack
Posted by Rukmi Patel | April 9, 2019
Comparing Full Stack V/S MEAN Stack approach
The web and mobile app development services have stepped up to next level in last 5 years. Web and apps are all built utilizing a ‘stack’ of various technologies like...
Posted by Rukmi Patel | September 6, 2018
Headless Drupal or DeCoupled Drupal
Now a days, front-end technologies like Angular, React.js, Backbone.js etc have started attracting audience because of its advantages over traditional rendering approach. Server side frameworks like Laravel, Node.js have already...