Doctrine update entity in loop, persist or flush?

2019-04-29 01:43发布

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条回答
该账号已被封号
2楼-- · 2019-04-29 02:19

You need only one flush, out of your loop:

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();

Calling $this->_em->persist($obj) involves to create a new entry.

If you need to create or update depending on the entry exists or not, look at EntityManager::merge .

To preserve memory usage for large number of entries, look at batch processing.

Note SensioLabs insight (PHP source code quality analysis) raises a warning if your code calls EntityManager::flush inside a loop.

查看更多
登录 后发表回答