我正在尝试将很多实体插入数据库。当我尝试使用此代码插入示例 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"}).
请问是什么问题?
顺便说一句:当我修改代码时,我只清除了 $notify 和 $notifiedUser ,例如:
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)
(所以有内存泄漏?)