Introduction to Transactions Management
Transaction management is a very important part of any web application or desktop application. Transaction allows you to safely apply the changes to the database done inside more than one process maintaining the consistency of the database. In simpler if you want either to apply all changes or none to the database, transaction is the solution for you. In this basic tutorial I’m going to explain how to use transactions with doctrine php orm.
Why we need Transactions
Consider a situation where your application provides functionality for register in the site and charges monthly for subscriptions. In this situation user has to provide profile details and then complete the payment to complete the registration process. In the back end we need to save both user details and payment details in the database on successful completion. Now think of what happens if the user profile details is saved and database failure occurs when the system try to save payment details. In this situation database will be in an inconsistent state and user will not be able to use the system even after making the payment. So to avoid such kind of situations we use transactions which allows you to save both details or none at all using commit and rollback functions.
Transactions Management with Doctrine PHP
$conn = Doctrine_Manager::connection();
There are 3 main methods used in transaction management.
1. Beginning a Transaction – Starts a transaction to execute
$conn->beginTransaction();
2. Committing a Transaction – Saves all the changes to the database
$conn->commit();
3. Rolling a Transaction Back – Removes all the changes from the database
$conn->rollback();
Example of using Transactions with Doctrine PHP
Example 1 – Saving 2 Different Objects Inside Transaction
$conn = Doctrine_Manager::connection(); $conn->beginTransaction(); try { $user = new User(); $user->username = 'testc151'; $user->password = 'fetr989'; $user->email = ''; $user->save(); $payment = new Payment(); $payment->amount = '200'; $payment->date = '01-03-2011'; $payment->save(); $conn->commit(); } catch(Exception $e) { $conn->rollback(); }
Data will be saved to database only if both user and payment details are in accepted format of database. Any error on saving user or payment will undo the changes to the database using rollback method.
Example 2 – Save and Update Objects Inside Transaction
$conn = Doctrine_Manager::connection(); $conn->beginTransaction(); try { $payment = new Payment(); $payment->amount = '200'; $payment->date = '01-03-2011'; $payment->save(); $update_user = Doctrine_Query::create() ->update('User') ->set('email ', '?', '') ->where('email = ?', ''); $update_user->execute(); $conn->commit(); } catch(Exception $e) { $conn->rollback(); }
Data will be saved to database only if both user and payment details are in accepted format of database. Any error on updating user or saving payment will undo the changes to the database using rollback method.
Example 3 – Save Object Collection Inside Transaction
$conn = Doctrine_Manager::connection(); $conn->beginTransaction(); try { foreach($user_array as $key=>$user){ $user->save(); } $conn->commit(); } catch(Exception $e) { $conn->rollback(); }
User objects inside the user array will be saved one by one to the database. If any of the user objects gets failed to save to the database, all the saved objects will be removed using rollback method.
Leave a Reply