代码之家  ›  专栏  ›  技术社区  ›  Lukas Lukac

实体出现symfony2 doctrine2批处理错误

  •  1
  • Lukas Lukac  · 技术社区  · 11 年前

    我正在尝试向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和$notified用户喜欢:

                    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 回复  |  直到 11 年前
        1
  •  0
  •   Ken Hannel    11 年前

    问题似乎来自这一行。看起来 $user 对象不是受管理的条令实体。

    $notify->setNotifyUser($user);