Symfony3 / Doctrine 2刷新错误

时间:2017-05-03 12:20:40

标签: php symfony doctrine-orm php-7

我创建了一个symfony命令,该命令应该导入2M条目并将它们插入数据库中。

我将刷新/清除并每500个条目产生一次,以便可以将其与学说一起插入并跟踪插入的进度。在我的机器上使用mySQL数据库服务器它可以工作,但在远程服务器上使用Postgresql它不起作用,我收到通知:

 [Symfony\Component\Debug\Exception\ContextErrorException]  
  Notice: Undefined index: 0000000025969b1e00000000029e7855  

Exception trace:
 () at /var/nginx/kweeri-next/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:2917
 Doctrine\ORM\UnitOfWork->getEntityIdentifier() at /var/nginx/kweeri-next/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:656
 Doctrine\ORM\Persisters\Entity\BasicEntityPersister->prepareUpdateData() at /var/nginx/kweeri-next/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:692
 Doctrine\ORM\Persisters\Entity\BasicEntityPersister->prepareInsertData() at /var/nginx/kweeri-next/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:271
 Doctrine\ORM\Persisters\Entity\BasicEntityPersister->executeInserts() at /var/nginx/kweeri-next/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1014
 Doctrine\ORM\UnitOfWork->executeInserts() at /var/nginx/kweeri-next/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:378
 Doctrine\ORM\UnitOfWork->commit() at /var/nginx/kweeri-next/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:356
 Doctrine\ORM\EntityManager->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

这是flush发出通知的代码。

 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(\AppBundle\Entity\PPS\Product::class);
        $existings = array_map(function (\AppBundle\Entity\PPS\Product $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 \AppBundle\Entity\PPS\Product($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并停止。

1 个答案:

答案 0 :(得分:2)

当您致电$em->clear()时,将清除整个身份地图。您需要从数据库重新加载所有对象才能使用它们,否则doctrine无法在标识映射中找到导致未定义索引错误的对象。

如果您知道对象ID并且不需要使用对象属性,则可以使用引用而不是从数据库加载对象。