symfony2 doctrine2批处理与实体的错误

时间:2013-09-27 18:01:26

标签: symfony doctrine-orm entity batch-processing

我正在尝试向DB插入大量实体。当我尝试使用此代码插入到示例5实体时,它可以正常工作...但是当我实现批处理方法时,它会导致此错误:

(代码)

        if (!empty($invite_this_peopleArray)) {
            $batchSize = 20;
            $i = 0;
            foreach ($explodeInviteArray as $explodingUser) {
                ++$i;

                $notifiedUser=$userRepo->find($explodingUser);

                $notify=new Notify();
                $notify->setNotifyUser($user);
                $notify->setUser($notifiedUser);
                $notify->setStatus($lastStatus);
                $notify->setTyp('invite');
                $notify->setViewed(false);
                $notify->setAdInfo($name);

                $em->persist($notify);

                if (($i % $batchSize) === 0) {
                    $em->flush();
                    $em->clear(); // Detaches all objects from Doctrine!
                }            
            }
        }

错误:

A new entity was found through the relationship 'TB\NotifyBundle\Entity\Notify#notifyUser' that was not configured to cascade persist operations for entity: (nick of notify user user($user->getUsername())). To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).

请问有什么问题?

BTW:当我修改代码时,我只清除$ notify和$ informedUser,如:

                if (($i % $batchSize) === 0) {
                    $em->flush();
                    $em->clear($notify); // Detaches all objects from Doctrine!
                    $em->clear($notifiedUser); // Detaches all objects from Doctrine!
                }   

错误消失了,但我尝试插入4000行,我发现了这个错误:

Fatal error: Maximum execution time of 30 seconds exceeded in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 514

当我尝试插入1000行时。

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)

(所以内存泄漏?)

1 个答案:

答案 0 :(得分:0)

看起来问题来自这条线。看来$user对象不是托管的Doctrine实体。

$notify->setNotifyUser($user);