Reputation: 3378
The doctrine interface Doctrine\Common\Persistence\ObjectManager
defines the flush
method as having no parameters. Yet the implementation Doctrine\ORM\EntityManager
allows a single entity to be passed.
Aside from the, IMO, bad programming style, Is this anything I need to be worried about?
I know that PHP will ignore any extra parameters if they are not declared in the method. Which would cause a non ORM manager to flush all entities.
I ask because I'm trying to write my code in such a way that the ORM is configurable and can switched at a later date. Now, while writing a batch import class, I have found that calling flush
without an entity causes memory leaks, it also effects a 'progress/history' entity I use outside of the main import loop. So it's pretty important I only flush certain entities.
Upvotes: 7
Views: 6416
Reputation: 289
I have noticed the differences between the definition and implementation of flush()
as well. That may be a question only the developers of doctrine can answer.
Don't worry about it.
We can still address the differences and how they affect your application.
According to doctrine's documentation, flush()
is the only method that will persist changes to your domain objects.
Other methods, such as persist()
and remove()
only place that object in a queue to be updated.
It is very important to understand that only EntityManager#flush() ever causes write operations against the database to be executed. Any other methods such as EntityManager#persist($entity) or EntityManager#remove($entity) only notify the UnitOfWork to perform these operations during flush. Not calling EntityManager#flush() will lead to all changes during that request being lost.
Flushing individual entities at a time may cause performance issues in itself. Each flush()
is a new trip to the database. Large sums of calls to flush()
may slow down your application.
The flush()
method should not be affecting your progress/history entity unless you are intentionally making changes to it. But, if that is the case, and you still do not want progress/history entity to be updated when flush()
is executed, you can detach the entity from doctrine. This will allow you to make changes to the entity without doctrine being aware of those changes. Therefore, it will not be affected by flush()
.
When you are ready for the entity to be re-attached to doctrine, you can use the merge method provided by your entity manager. Then call flush()
one last time to merge the changes.
Upvotes: 2