2013-09-27 44 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