read objects persisted but not yet flushed with doctrine

前端 未结 3 714
慢半拍i
慢半拍i 2021-01-03 22:57

I\'m new to symfony2 and doctrine. here is the problem as I see it. i cannot use :

$repository = $this->getDoctrine()->getRepository(\         


        
3条回答
  •  借酒劲吻你
    2021-01-03 23:49

    I think you might look at the problem from the wrong angle. Doctrine is your persistance layer and database access layer. It is the responsibility of your domain model to provide access to objects once they are in memory. So the problem boils down to how do you get a reference to an object without the persistance layer?

    Where do you create the object you need to get hold of later? Can the method/service that create the object return a reference to the controller so it can propagate it to the other place you need it? Can you dispatch an event that you listen to elsewhere in your application to get hold of the object?

    In my opinion, Doctrine should be used at the startup of the application (as early as possible), to initialize the domain model, and at the shutdown of the application, to persist any changes to the domain model during the request. To use a repository to get hold of objects in the middle of a request is, in my opinion, probably a code smell and you should look at how the application flow can be refactored to remove that need.

提交回复
热议问题