我想要插入大量的实体到数据库。当我尝试插入到例如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