2013-09-27 29 views
1

我想要插入大量的实体到数据库。当我尝试插入到例如5个实体与此代码它的工作原理没有问题...但是当我实现了批量方法有它会导致这个错误:symfony2 doctrine2与实体的批处理错误

(代码)

 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:如果我修改了代码,所以我只清除$通知和$ notifiedUser,如:

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

错误是走了,但我尝试插入4000行,我得到insted的这个错误:

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) 

(所以有什么地方内存泄漏?)

+0

我曾与APC,我所做的是改变配置它php.ini文件允许它使用更多的内存这里面完全一样的问题。 – Splendonia

回答

0

它看起来像这个问题是从这一行来。它会出现$user对象不是管理的主体实体。

$notify->setNotifyUser($user);

+0

我编辑了我的问题。请检查一下。 – EnchanterIO

+0

最大执行时间可能是PHP配置问题。我会建议检查你的PHP配置并且增加最大脚本执行时间。 –

+0

即使这可行......但我不认为它的脚本运行这么长时间......脚本是邀请组合功能,所以经常运行......任何其他想法? – EnchanterIO