Also, remember that the flush method actually updates your changes to the database. This means that if you need to save several objects, it is better to make one reset at the end of each save operation.
Say you have three objects: $entity1 , $entity2 and $entity3 . Then this code is not optimal:
$em->persist($entity1); $em->flush(); $em->persist($entity2); $em->flush(); $em->persist($entity3); $em->flush();
This code will work better because it will only sync your database once:
$em->persist($entity1); $em->persist($entity2); $em->persist($entity3); $em->flush();
Therefore, having a shortcut in your controller, such as persistAndFlush , should be used with caution, because it is inefficient when you need to save multiple objects. This is stated here in the Doctrine2 documentation (3/4 page). Here's a text taken from official documentation 2.0:
Do not call a flash after every change to the entity or every single call to persist / remove / merge / ... This is an anti-template and unnecessarily reduces the performance of your application. Instead, form units of work that work with your objects and trigger a flash when you are done. When servicing a single HTTP request, it is usually not necessary to call a reset more than 0-2 times.
Regards, Matt
source share