【问题标题】:Inject EntityManager into Dependendy Injection symfony将EntityManager注入依赖注入symfony
【发布时间】:2014-09-23 06:44:45
【问题描述】:

我正在尝试更改我的实体管理器的这个结构启动以使用 Symfony 组件依赖注入。

目前正在初始化它。

$paths = array(__DIR__ . "/app/Entity/");

$isDevMode = true;

$conn = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => 'senha',
    'dbname'   => 'db_teste',
    'host'     => 'localhost',
    'port'     => '3306',  
); 

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$cachingBackend = new \Doctrine\Common\Cache\FilesystemCache('/tmp/doctrine2');
$config->setMetadataCacheImpl($cachingBackend);
$config->setQueryCacheImpl($cachingBackend);
$config->setResultCacheImpl($cachingBackend); 

$em = EntityManager::create($conn, $config);

此时我的 entitymanager 已经可以使用了。

现在我启动了我的容器。

$container = new ContainerBuilder();

但我不知道如何将 entitymanager 作为服务注入。

我已经阅读了足够多的文档,但是当我们遇到静态方法的情况时,没有说明如何做到这一点。

有人帮帮我吗?

谢谢!

【问题讨论】:

    标签: symfony dependency-injection doctrine-orm entitymanager


    【解决方案1】:

    怎么样:

    ...
    // by looking into the EntityManager class, I see that the
    // constructor takes a mandatory eventManager in contrary
    // to the static create method, where the EventManager is optional.
    $eventManager = new Doctrine\Common\EventManager();
    
    $container->register('entityManager', 'Doctrine\ORM\EntityManager')
    ->setArguments(array($conn, $config, $eventManager));
    

    【讨论】:

    • 我听从了你的建议,得到了这条消息:PHP 致命错误:未捕获的异常 'ReflectionException' 带有消息'访问类 Doctrine\\ORM\\EntityManager 的非公共构造函数'
    猜你喜欢
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    相关资源
    最近更新 更多