Php EntityManager已关闭:我是否必须先检查所有项目?

Php EntityManager已关闭:我是否必须先检查所有项目?,php,symfony,doctrine,Php,Symfony,Doctrine,在很多框架中,当您在数据库中执行请求时,tou可以捕获数据库错误以抛出“用户友好”错误 当我使用条令时,例如,我插入了一个重复的值,我有一条消息“EntityManager已关闭”,无法继续 在尝试创建实体之前,我们是否总是必须检查重复项、外键 例如,我在一个服务中这样做: ... foreach ($reponse as $item) { $item = new Item(); $item->setRelationId(item->id); $item-&g

在很多框架中,当您在数据库中执行请求时,tou可以捕获数据库错误以抛出“用户友好”错误

当我使用条令时,例如,我插入了一个重复的值,我有一条消息“EntityManager已关闭”,无法继续

在尝试创建实体之前,我们是否总是必须检查重复项、外键

例如,我在一个服务中这样做:

...
foreach ($reponse as $item) {
    $item = new Item();
    $item->setRelationId(item->id);
    $item->setValue($item->value);
    ...
    try {
        $this->em->persist($item);
        $this->em->flush();
    } catch (\Exception $e) {
        // Useless because if there is an exception, after this the kernel handler ends the transaction
    }
}
简单地用教义做这件事的好方法是什么


谢谢

您可以检查是否已关闭,如果已关闭,您可以像这样重新打开它,例如:

   if (!$this->em->isOpen()) {
        $this->em = $this->em->create(
            $this->em->getConnection(),
            $this->em->getConfiguration()
        );
    }

谢谢你的回答。但是当连接关闭时,我必须重新打开调用我的服务的类的每个连接?这段代码在一个方法中,但我必须将youss解决方案粘贴到调用此方法的类中。我说的对吗?如果entitymanager关闭,您可以在foreach中进行检查,这是在您执行许多操作时发生的,因此您不需要每次都进行检查,但我认为在这个foreach中是必需的