read objects persisted but not yet flushed with doctrine

前端 未结 3 715
慢半拍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:30

    I think you might want to have a look at this function:

    $entityManager->getUnitOfWork()->getScheduledEntityInsertions()
    

    Gives you back a list of entity objects which are persisting yet.

    Hmm, I didn't really read your question well, with the above you will retrieve a full list (as an array) but you cannot query it like with getRepository. I will try found something for u..

    0 讨论(0)
  • 2021-01-03 23:46

    Your is a business logic problem effectively.

    Querying down the Database a findby Query on Object that are not flushed yet, means heaving much more the DB layer querying object that you have already in your function scope.

    Also Keep in mind a findOneBy will retrieve also other object previously saved with same features.

    If you need to find only among those new created objects, you should make f.e. them in a Session Array Variable, and iterate them with the foreach.

    If you need a mix of already saved items + some new items, you should threate the 2 parts separately, one with a foreach , other one with the repository query!

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题