Transactions and Symfony2 Manager

Is there a way to manually specify transactions in symfony2 with an entity manager (doctrine), or maybe a natural way to execute in one transaction what I do below in two?

// creating screen object... //Creating user object... //flush the screen into database in order to get the Id to relate the server (user) to $em->persist($screen); $em->flush(); //Get id of just inserted screen and attach that to new server (user) $tempRecordId = $screen->getId(); $tempEntity = $em->getRepository('BizTVContainerManagementBundle:Container')->find($tempRecordId); $entity->setScreen($tempEntity); //Flush the user also into database $em->persist($entity); $em->flush(); 

See that I have to clear my first object to get its ID, so I can associate my second entity with my first ...

+8
database symfony doctrine sqltransaction
source share
2 answers

Why don't you just do:

 // creating screen object... //Creating user object... $entity->setScreen($screen); $em->persist($screen); $em->persist($entity); $em->flush(); 
+7
source share
 try { $em->getConnection()->beginTransaction(); // do your thing here $em->getConnection()->commit(); } catch (\Exception $e) { $em->getConnection()->rollback(); throw $e; } 
+22
source share

All Articles