Last active
August 25, 2017 09:03
-
-
Save matstani/9304923 to your computer and use it in GitHub Desktop.
ZF2 トランザクションサンプル
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 | |
//... | |
//tableGatewayインスタンスからAdapterを取得する場合 | |
$adapter = $this->tableGateway->getAdapter(); | |
//ServiceManagerからAdapterを取得する場合 | |
$sm = $this->getServiceLocator(); | |
$adapter = $sm->get('Zend\Db\Adapter\Adapter'); | |
$conn = $adapter->getDriver()->getConnection(); | |
try { | |
//トランザクション開始 | |
$conn->beginTransaction(); | |
//データベース処理 | |
//トランザクションコミット | |
$conn->commit(); | |
} catch (\Exception $e) { | |
//トランザクションロールバック | |
$conn->rollback(); | |
error_log($e->getMessage()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment