i am trying to insert a lot of entities to DB. When i try to insert to example 5 entities with this code it works without problem... but when i implemented the batch method there it causes this error:
(code)
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!
}
}
}
Error:
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"}).
What is the problem please?
BTW: when i modified the code so i clear only $notify and $notifiedUser like:
if (($i % $batchSize) === 0) {
$em->flush();
$em->clear($notify); // Detaches all objects from Doctrine!
$em->clear($notifiedUser); // Detaches all objects from Doctrine!
}
The error is gone but i try to insert 4000 rows and i am getting insted this error:
Fatal error: Maximum execution time of 30 seconds exceeded in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 514
When i try insert 1000 rows.
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)
(so there is somewhere memory leak? )
It looks like the issue is coming from this line. It would appear the the $user
Object is not a managed Doctrine Entity.
$notify->setNotifyUser($user);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments