Undefined index error upon $em->clear() in Symfony2
Symfony logs all SQL queries in dev environment, so first you need to disable it
// disable logger$em->getConnection()->getConfiguration()->setSQLLogger(null);
You may use event listeners on entities, it may also increase memory usage. You can disable them like so
// remove all listenersforeach ($em->getEventManager()->getListeners() as $event => $listeners) { foreach ($listeners as $listener) { $em->getEventManager()->removeEventListener($event, $listener); }}
Remove unset
from your code, there is no need for them, as you clear entity manager every step of your loop.
// save and clear$em->flush();$em->getUnitOfWork()->clear();
Remember that doctrine can optimize your queries, and improve perfomance if you group queries into one flush
. So the best practice would be to execute flush
once over some parts of your data. For example:
// collect 100 entities and then save themif (($i % 100) == 0) { $em->flush(); $em->getUnitOfWork()->clear();}
Try to reset the entity manager with:
$this->getContainer()->get('doctrine')->resetEntityManager();
and then:
$em = $this->getContainer()->get('doctrine')->getEntityManager();