This tutorial will show you how we can use transaction in Codeigniter. We know that transaction is an important part of persistence storage media because we may save, update or delete data from persistence storage area. For example, in database we may need to update multiple tables or we may require to delete from multiple tables, hence we may face some inconsistency during database tables updation or deletion from database tables. During updation few tables may be updated and few tables may not be updated. Similarly, in case of deletion few tables may get deleted and few may not. So data will be in inconsistent state. Therefore, we want either a unit of work will be carried out completely or none. Hence the transaction is required to make it happen.
For information on Codeigniter transaction please go through https://ellislab.com/codeigniter/user-guide/database/transactions.html

Prerequisites

Knowledge of PHP & Codeigniter
MySQL 5.x
Apache HTTP server 2.2
Codeigniter 2.1.4

Now look at the steps how transaction works in Codeigniter

Step 1. Create MySQL tables and these tables will be used for updation or deletion of data.

Step 2. Create usermodel.php file for updating to or deleting data from database

That’s all. Thanks for reading.

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

9 Thoughts on “Codeigniter Transaction Example”

  • Hello Soumitra,
    Thanks for all your examples

    Is there any continuation for the above tutorial..? if yes please help us in CI itself.

  • Hello,

    What if I need to use the delete function somewhere inside the update function?
    I mean how to use a transaction enabled function inside another transaction enabled function. I tried and it fails.

    • why do you need to use another transaction enabled function inside other and if you need to use a function from transaction enabled function then why do you need to make the called function transactional?

      • Hi,
        Thanks for your attention. Let me explain.

        Suppose there is a function that creates new “Task” that belongs to a particular “Project”. We have a form that submits the task details the user who is assigned to it, and stores task details to task_table and some other tables that stores the relations between task and project or the users table that are assigned to this task for example. So I put it in the transaction rollback. This is independent now.

        When I create a new project, it may be needed to store the project id to some third table. Also I need to create some default tasks that will be related to this particular project. Now the project function has multiple transactions. It will rollback the independent task query but the project will not be rolled back and we have created a project without those default tasks.

        I hope you got my point.

        Thanks again,
        Devesh

  • Sis,

    I’m new in php and ci but I’m still learning from online resources. Can you create a full tutorial in CRUD using transaction. Thanks for helping 🙂

    • You need transaction on write, update and delete but not in read. So in this example you will find transactions on update and delete, similarly you can apply on write. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.