php - Symfony 3.4 - EntityManager 已关闭

标签 php mysql symfony doctrine-orm orm

我有一个使用 Symfony 3.4 和 MySql 运行的应用程序,但出现错误:“EntityManager 已关闭”。我的应用程序以两种方式运行:

1 - 每次都由 sh 脚本调用的控制台应用程序。该控制台应用程序在数据库表中进行了多次插入。

2 - 也插入同一个表中的 HTTP 路由。

当控制台应用程序在后台运行时,如果我调用 http 路由,我会收到错误“EntityManager 已关闭”。如果我停止后台应用程序,http 路由就可以工作。就好像两个应用程序控制台和 http 使用同一个实例 EntityManager。

我的代码:

我创建一个名为 AbstractRepositoryService 的服务。我所有管理存储库的服务都应该扩展。

<?php

abstract class AbstractRepositoryService
{

    /**
     *  
     * @var EntityManagerIntergace - $em
    */
    protected $em;

    /**
     * 
     * @param EntityManagerInterface $em
    */
    public function __construct(EntityManagerInterface $em) {

        $this->em = $em;
    }

    /**
     *
     * 
     * @param String
     * 
     * @return @mixed
     * 
     * @throws RuntimeException
    */
    public function __call($method, $args) {

        $repository = $this->em->getRepository(static::ENTITY);

        if (!method_exists($repository, $method)) {

            throw new RuntimeException(
                sprintf("Method '%s' not found.", $method), 
                500
            );
        }

        try {

            return call_user_func_array(array($repository, $method), $args);

        } catch(Exception $e) {

            throw new Exception($e->getMessage(), 500);
        }

    }
}

我的 UserRepositoryService 在 flush 方法中抛出异常

<?php

final class UserRepositoryService extends AbstractRepositoryService
{
    /**
     * 
     * @const String
    */
    const ENTITY = 'AppBundle\\Entity\\User';

    /**
     * 
     * @param User
    */
    public function insert(User $user) {

        try {

            $this->em->persist($user);
            $this->em->flush($user);

        } catch (Exception $e) {

            throw new Exception($e->getMessage(), 500);
        }
    }
}

最后是我的服务声明:

app.services.user_repository_service:
        public: true
        class: AppBundle\Services\UserRepositoryService
        arguments:
            - '@doctrine.orm.entity_manager'

最佳答案

解决了!

我创建了一个在插入之前生成新 EntityManager 的方法,现在可以使用。

protected function createNewEntityManager() {

    return $this->em->create(
        $this->em->getConnection(),
        $this->em->getConfiguration(),
        $this->em->getEventManager()
    );
}

并插入:

public function insert(Crawler $crawler) {

    try {

        $this->createNewEntityManager();

        $this->em->persist($crawler);
        $this->em->flush($crawler);
        $this->em->close();

    } catch (Exception $e) {

        throw new Exception($e->getMessage(), 500);
    }
}

关于php - Symfony 3.4 - EntityManager 已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49556881/

相关文章:

php - 显示它们之前如何从数据库中订购获取的对象?

php - SQL从一个列复制数据到另一个表的指定列

MySQL - MySQL 用户定义函数的正确语法

mysql - Tableau-如何处理从聚合数据创建的数据子集

php - Symfony2 中 isGranted 的替代品

php - Symfony2 : how to set the host/base url in CLI scripts

php - Yii2 ActiveRecord 模型最佳实践

php - 处理 $_POST[] 和 $_GET[] 变量

php - 上传文件并将文件名保存在 MySQL 数据库中

php - Class 似乎不是一个托管的 Doctrine 实体。你忘记映射了吗?