我想要插入大量的實體到數據庫。當我嘗試插入到例如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)
(所以有什麼地方內存泄漏?)
我曾與APC,我所做的是改變配置它php.ini文件允許它使用更多的內存這裏面完全一樣的問題。 – Splendonia