-
-
Save ahmed-bhs/f92b0ec9458af6608aa45ace7843dc6e to your computer and use it in GitHub Desktop.
Transactions in Symfony 2 and Doctrine 2
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
<?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