我创建了一个Symfony命令,该命令应导入2M条目并将其插入数据库。
我放了一个冲洗/清除,并每500个条目产生每500个条目,使其可以用学说插入并跟踪插入的进展。在使用MySQL数据库服务器的计算机上,它可以正常工作,但是在带有PostgreSQL的遥远服务器上,它不起作用,我会注意到:
[SymfonyComponentDebugExceptionContextErrorException]
Notice: Undefined index: 0000000025969b1e00000000029e7855
Exception trace:
() at /var/nginx/kweeri-next/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:2917
DoctrineORMUnitOfWork->getEntityIdentifier() at /var/nginx/kweeri-next/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:656
DoctrineORMPersistersEntityBasicEntityPersister->prepareUpdateData() at /var/nginx/kweeri-next/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:692
DoctrineORMPersistersEntityBasicEntityPersister->prepareInsertData() at /var/nginx/kweeri-next/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:271
DoctrineORMPersistersEntityBasicEntityPersister->executeInserts() at /var/nginx/kweeri-next/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1014
DoctrineORMUnitOfWork->executeInserts() at /var/nginx/kweeri-next/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:378
DoctrineORMUnitOfWork->commit() at /var/nginx/kweeri-next/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:356
DoctrineORMEntityManager->flush() at /var/nginx/kweeri-next/var/cache/dev/appDevDebugProjectContainer.php:6083
DoctrineORMEntityManager_00000000612352fa000000006464aa610448bf5c446411a05ad87329e18cead6->flush() at /var/nginx/kweeri-next/src/AppBundle/Utility/Clients/IRI/PPS/ImportItems.php:219
这是齐平发出通知的代码。
public function importDictionnary($companyMachineName, $file) {
$company = $this->em->getRepository("AGAdminBundle:Company")->findOneBy(["machineName" => $companyMachineName]);
if (!$company) {
throw new EntityNotFoundException("no company found for $companyMachineName");
}
$eans = $file;
$this->logger->addInfo("File downloaded");
$product_repo = $this->em->getRepository(AppBundleEntityPPSProduct::class);
$existings = array_map(function (AppBundleEntityPPSProduct $product) {
return $product->getEan();
}, $product_repo->findBy(["company" => $company]));
$old_value = -1;
foreach ($eans as $k => $ean) {
if (!in_array($ean, $existings)) {
$this->em->persist(new AppBundleEntityPPSProduct($ean, $company));
if ($k % 500 == 0) {
yield round(($k / count($eans)) * 100);
$this->em->flush();
$this->em->clear();
}
}
}
$this->em->flush();
}
编辑:需要30秒的时间才能在我的机器上产生2个。在遥远的机器上,最多需要20分钟才能产生2并停止。
调用$em->clear()
时,整个身份图将被清除。您需要从数据库中重新加载所有对象才能与它们一起使用,否则学说无法在身份图中找到导致索引错误的对象。
如果您知道对象ID并且不需要使用对象属性,则可以使用参考而不是从数据库加载对象。