SQL Transaction in Cakephp 3

Author - Parminder Kaur
16.10.2017
|
0 Comments
|

SQL transaction in Cakephp 3 is a series of statements working in a logical order to handle database errors and ensure data integrity. There are four main methods being used in Sql transaction in Cakephp 3.

1. Initialize the statement using begin() method.

2. Execute the statement which includes database query using execute() method.

3. To save the changes using commit() method.

4. If some error occured while saving the data or transaction does not get completed then the changes will be reverted using rollback() method.

In Cakephp 3, database connections are configured in config/app.php. All this connection information is stored in Connection Manager which establishes the connection and is used by the Cakephp application.

Example :

use Cake\Datasource\ConnectionManager;
$connection = ConnectionManager::get('default');
try{
$connection>begin();
$stmt = $connection>execute(
'UPDATE users SET user_name = ? WHERE id = ?',
[$this->request->data[‘user_name’],$this->request->data['id']],
['text','integer']
);

$connection>commit();
}catch(Exception $e){
$connection>rollback();
}
Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published.