多くのエンティティを DB に挿入しようとしています。このコードを使用してサンプル 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)
(ということは、どこかにメモリリークがあるのでしょうか?)