Skip to content

Instantly share code, notes, and snippets.

@ursuleacv
Created February 14, 2014 17:46
Show Gist options
  • Save ursuleacv/9005588 to your computer and use it in GitHub Desktop.
Save ursuleacv/9005588 to your computer and use it in GitHub Desktop.
Using Transaction with Active Record in Yii
$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