What is the best practice regarding calling flush() method of Doctrine? #10942
iliayatsenko
started this conversation in
Polls
Replies: 1 comment 1 reply
-
I would say, "Other".
The For multiple you can flush multiple as following: foreach($changedEntities as $entity) {
$em->persist($entity);
}
$em->flush(); // Will flush only marked through `persist($entity)`. (Of course, need to consider amount and performance) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In Doctrine ORM there is a separation between
persist()
andflush()
operations. Looks like whenever you want to insert/update your entity, you have to call two methods one by one. And most developers do it this way. But I wonder if it was intended to be used this way.Most of the time I see repositories like this:
And this approach has a big flaw, even if leave verbosity aside - call to
flush()
will sync ALL the not-synced entities with database, not only the one we are saving now. I would suggest to callto restrict sync to only one entity, but this approach was deprecated by Doctrine.
In my understanding,
flush()
was intended to be called only once in "business transaction", and it's responsibility is to sync all the in-memory changes with database. And this adheres well to the "functional" idea of making domain core "pure" of any side effects: we just move all side effects outside the domain code. Am I wrong? What drawbacks has this approach?What is the best practice of dealing with
flush()
?16 votes ·
Beta Was this translation helpful? Give feedback.
All reactions