将EntityManager注入依赖注入symfony

时间:2014-07-31 10:45:15

标签: symfony dependency-injection doctrine-orm entitymanager

我正在尝试更改我的实体管理器的此结构启动以使用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);

此时我已经可以使用我的实体管理器了。

现在我开始了我的容器。

$container = new ContainerBuilder();

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

我已经阅读了足够的文档,但是当我们遇到静态方法的情况时,没有显示如何执行此操作。

有人帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

怎么样:

...
// 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));
相关问题