You need only one flash from the loop:
foreach ($bets as $key => $bet) { $devices = $this->em->getRepository('AppBundle:Device')->findBy(array('user' => $bets->getUser())); foreach ($devices as $key => $device) {
Calling $this->_em->persist($obj) involves creating a new record.
If you need to create or update depending on an existing record, look in EntityManager::merge .
To save memory usage for a large number of records, review batch processing .
Note SensioLabs analysis (PHP source code quality analysis) raises a warning if your code calls EntityManager::flush inside a loop.
source share