Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 重置实体管理器的最佳方法_Php_Symfony - Fatal编程技术网

Php 重置实体管理器的最佳方法

Php 重置实体管理器的最佳方法,php,symfony,Php,Symfony,当我遇到异常时,重置条令实体管理器的最佳方法是什么 在foreach循环中,我需要保存我得到的最大实体数。如果一个实体失败,我想发送一封电子邮件并继续运行foreach循环 例如: private $entityManager; public function __construct(EntityManagerInterface $entityManager) { $this->entityManager = $entityManager; } public function

当我遇到异常时,重置条令实体管理器的最佳方法是什么

在foreach循环中,我需要保存我得到的最大实体数。如果一个实体失败,我想发送一封电子邮件并继续运行foreach循环

例如:

private $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
    $this->entityManager = $entityManager;
}

public function testMethod(array $entities)
{
    foreach ($entities as $entity)
    {
        try 
        {
            $entity = new MyEntity();
            $entity->setSomeData();
            $this->entityManager->persist($entity);
            $this->entityManager->flush();
        } catch (\Exception $e)
        {
          //SEND EMAIL
        }
    }
}
有一段时间,我收到消息“EntityManager已关闭”,由一个异常抛出

重置实体管理器以保持循环的最佳方式是什么


注意:这是一个示例代码。我不会为每一个错误发送电子邮件或刷新。

您可以做一些事情来提高性能:您只需在循环外呼叫。因此,只有在所有存储结束时才会刷新内存<代码>$this->entityManager->flush()


但是您可以尝试调用
$this->getDoctrine()->resetManager()如果在抛出异常后需要继续保存内容。

是否会出现错误?似乎这不应该是你想要的行为?为什么你不在为每一个人洗脸?为什么不等到你知道最后的结果,然后发送一封电子邮件,说明在单次刷新过程中可能发生的情况?我不明白您在这里真正想要实现的目标。我有很多约束验证,但有时,一个错误可能会阻止保存。我不能仅仅因为一两个坏实体就停止这个过程。我更喜欢手动修复。我认为这种想法通常是糟糕的设计。。。如果数据库关闭,您确定要接收大量邮件吗?相反,失败的部分应该抛出代码中捕获的异常,并继续循环。最后坚持一次,然后发送包含所有丢失项目的邮件。我只发送一封电子邮件。这只是一个例子;)好的,我知道了。如果您仅在foreach的末尾调用flush,那么如果抛出异常,您将无法一次保存所有内容。但是试着重置管理器,也许它会起作用。如果您使用的是实体管理器,您如何重置管理器?您将无法调用$this->getDoctrine()..@Angel您必须在构造函数中注入ManagerRegistry并调用$registry->resetManager()@Angel将
doctrine
注入服务是否有问题?