Skip to content

Instantly share code, notes, and snippets.

@KaduNovoK
Created January 8, 2014 11:42
Show Gist options
  • Save KaduNovoK/8315647 to your computer and use it in GitHub Desktop.
Save KaduNovoK/8315647 to your computer and use it in GitHub Desktop.
Transactions in Symfony 2 and Doctrine 2
<?php
// Code sample to use Transactions
// Get the Entity Manager
$em = $this->getDoctrine()->getEntityManager();
// Suspend the auto-commit
$em->getConnection()->beginTransaction();
try {
// Get the entity
$entity = $this->getDoctrine()->getRepository('YourBundle:EntityName')->find($id);
// Code changing the entity properties...
// Save the Entity
$em->persist($entity);
$em->flush();
// Commit the transaction
$em->getConnection()->commit();
} catch (Exception $e) {
// Rollback on error
$em->getConnection()->rollback();
throw $e;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment