![]() |
using transactions - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: using transactions (/showthread.php?tid=65701) |
using transactions - sneakyimp - 07-13-2016 I've read the docs on db transactions and also the db driver reference to try and get a grip on how transactions work in CodeIgniter and, although I have often used MySQL transactions, I'm still a bit confused. In particular, it doesn't look like CI doesn't offer any function to force a ROLLBACK. I understand from the docs that "queries will all be committed or rolled back based on success or failure of any given query" but I do not see how I might force a rollback based on some non-query condition. Can someone explain how I can force a rollback? The docs refer to trans_begin but this function is not documented in the db driver reference. Most importantly, I want to know how to use transactions in my registration handler. When a user registers, I need to run some functions and insert records into 3 different db tables and I need to check some other stuff too. My transaction section looks something like this: PHP Code: try { Can someone help me understand how to use transactions in this context? RE: using transactions - mwhitney - 07-15-2016 You probably need to run the transactions manually in this context. In other words, you start with trans_begin() instead of trans_start(), then use trans_rollback() and trans_commit() to control whether the transaction is rolled back or committed. You also don't use trans_complete() for manual transactions. When running the transactions manually, remember to check trans_status() before calling trans_commit(). Essentially, this is what trans_complete() normally does (chooses whether to call trans_rollback() or trans_commit() based on the result of trans_status()), but controlling the transactions manually allows you to rollback or commit the transaction based on your own logic. |