Created
April 9, 2013 17:24
-
-
Save x5gtrn/5347599 to your computer and use it in GitHub Desktop.
Transaction Management by Model for CakePHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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