How should flushing be handled in a doctrine EntityManager instance shared across different services in symfony2?

Posted by Jbm on Stack Overflow See other posts from Stack Overflow or by Jbm
Published on 2012-03-19T23:26:12Z Indexed on 2012/03/19 23:29 UTC
Read the original article Hit count: 160

Filed under:
|

I have defined several services in symfony 2 which persist changes to the database. These services have the doctrine instance as one of their dependencies:

a.given.service:
    class: Acme\TestBundle\Service\AGivenService
    arguments: [@doctrine]

If I have two different services and both of them persist objects through the EntityManager, which is obtained like this from the doctrine instance:

$em = $doctrine->getEntityManager();

Would all services always share the same EntityManager? If so, how should I handle flushing if I wanted to handle all the changes in a single transaction? I have checked this: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/transactions-and-concurrency.html and it explains how to handle different transactions in a request, but I want to achieve the opposite, which is having different changes in different services handled as a single transaction.

Is there a better approach to handle multiple changes in different services?

For now my best bet is having a front-end service in charge of calling the other services and doing the flushing afterwards. Backend services would persist objects but would not do any flushing.

© Stack Overflow or respective owner

Related posts about doctrine

Related posts about symfony-2.0