Doctrine update entity in loop, persist or flush?

Viewed 9176

I have multiple loops like :

    $bets = $this->em->getRepository('AppBundle:Bet')->getBetsForMatch($match_id);

    foreach ($bets as $key => $bet) {
        $devices = $this->em->getRepository('AppBundle:Device')->findBy(array('user' => $bets->getUser()));

        foreach ($devices as $key => $device) {
            //HERE I SEND A PUSH NOTIFICATION

            if($this->rms_push->send($message)){
                $device->getUser()->setBadge($device->getUser()->getBadge() + 1);
                $this->em->flush();
            }
        }
    }

So, I get all bets for a match, for each bet I get all devices saved for the user, and after that I need to update my user with : $device->getUser()->setBadge($device->getUser()->getBadge() + 1);

For now, I flush each time but I think there is a better way, ideas ?

1 Answers
Related