How does CodeIgniter 4 handle Transactions?
Why Interviewers Ask This
This tests whether you can apply CodeIgniter knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.
Answer
Database transactions in CodeIgniter 4 ensure data integrity for operations that must all succeed or all fail together. Manual control: $db = \Config\Database::connect(); $db->transStart(); $db->table("orders")->insert($orderData); $db->table("inventory")->where("id", $itemId)->decrement("stock", $qty); if ($db->transStatus() === false) { $db->transRollback(); } else { $db->transComplete(); }. Automatic transactions (throw exception on failure): $db->transException(true)->transStart(); ... $db->transComplete(). Strict mode (enabled by default): if one query fails, all subsequent queries in the transaction fail too. Disable strict: $db->transStrict(false). Nested transactions use a counter — the outer transComplete() is the actual commit. Transactions are essential for financial operations, inventory management, and any multi-table writes that must be atomic.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last CodeIgniter project, I used this when...' immediately makes your answer more credible and memorable.