Created
February 14, 2014 17:46
-
-
Save ursuleacv/9005588 to your computer and use it in GitHub Desktop.
Using Transaction with Active Record in Yii
This file contains hidden or 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
$model=Post::model(); | |
$transaction=$model->dbConnection->beginTransaction(); | |
try | |
{ | |
// find and save are two steps which may be intervened by another request | |
// we therefore use a transaction to ensure consistency and integrity | |
$post=$model->findByPk(10); | |
$post->title='new post title'; | |
if($post->save()) | |
$transaction->commit(); | |
else | |
$transaction->rollback(); | |
} | |
catch(Exception $e) | |
{ | |
$transaction->rollback(); | |
throw $e; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment