Skip to content

Instantly share code, notes, and snippets.

@x5gtrn
Created April 9, 2013 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x5gtrn/5347599 to your computer and use it in GitHub Desktop.
Save x5gtrn/5347599 to your computer and use it in GitHub Desktop.
Transaction Management by Model for CakePHP
<?php
App::uses('TransactionManager', 'Model');
class DBMainTransactionManager extends TransactionManager {
public $name = 'DBMainTransactionManager';
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct();
$this->useDbConfig = Configure::read('Datasource.mysql.master.main');
}
}
<?php
App::uses('AppModel', 'Model');
abstract class TransactionManager extends AppModel {
public $name = 'TransactionManager';
public $useTable = false;
public function begin() {
$ret = $this->getDataSource()->begin();
if ($ret === false) {
throw new TransactionException(
'Transaction can not be begun.',
0x00);
}
}
public function commit() {
$ret = $this->getDataSource()->commit();
if ($ret === false) {
throw new TransactionException(
'Transaction could not be committed.',
0x00);
}
}
public function rollback() {
$this->getDataSource()->rollback();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment