Sometimes we need to use the database transaction for saving the data in the tables for a complex application. Like booking online tickets for any show, movies, bus and planes.
In this case there will be some probability that the data will not be saved in the database due to a reason, like network failure, database inconsistency or table integrity etc.
There might be a case where data is saved partially in some tables and will create data inconsistency. The best example we can take here is ATM. i.e. you got a message that the amount has deducted from your account but you did not receive the money from the ATM.
To handle situations like this the CakePHP 2.x provides a feature transaction. Using transactions we cannot avoid the situation but can protect the harm it causes.
To perform a transaction, a model’s table must be of a data source and type which supports transactions.
All transaction methods must be performed on a model’s Data Source object. To get a model’s Data Source from within the model use:
$dataSource = $this->getDataSource();
You can then use the data source to start, commit, or roll back transactions.
</pre> //Below code is responsible for begin the transaction $dataSource->begin(); // Perform some tasks if (/*all's well*/) { //This will commit the transaction on successful operation $dataSource->commit(); } else { //This will rollback the above operation in failure $dataSource->rollback(); }
Ex.
Let’s say we have a situation where we want to create a lead from the contact and send email to a customer after the contact gets saved in the database successfully.
Look at the below code to handle this situation.
So in CakePHP we have a model for tables. So for the contacts table we have “Contact.php” inside the model folder. Inside the “Contact.php” model, write the below code to handle transactions.
//Transaction example public function saveContactAndSendEmail() { $dataSource = $this->getDataSource(); try{ $dataSource->begin(); $aar['first_name'] = 'John'; $aar['last_name'] = 'Doe'; $aar['email'] = '[email protected]'; if(!$this->save($aar)){ throw new Exception(__('Failed to save contacts data.')); }else{ //Write the code to create a lead $leadArr['lead_first_name'] = 'John'; $leadArr['lead_last_name'] = 'Doe'; $leadArr['lead_email'] = '[email protected]'; $Lead = ClassRegistry::init('Lead'); if(!$Lead->save($leadArr)){ throw new Exception(__('Failed to save lead detail.')); }else{ //send email that lead is created } } $dataSource->commit(); }catch(Exception $e) { //echo $e->getMessage(); $dataSource->rollback(); } }
Contents
Conclusion:
To perform a transaction, a model’s table must be of a data source and type which supports transactions. All transaction methods must be performed on a model’s data source object.
At Andolasoft we are very much experienced in handling the transmission issues in database. Consult now to solve your issues relating to same and other challenges in CakePHP. We have a team of experienced and dedicated CakePHP developers to help you. Hire us now!