2

I am developing a small application outside of the Symfony framework and I want to use the Symfony dependency injection component to automatically resolve my repositories which require \Doctrine\ORM\EntityManager as a parameter in their constructor.

I want to bind an existing instance to the dependency injection container - but I can't seem to figure this out from the documentation. It seems that the container is using reflection to get the class name of the instance and then create a new instance. The variable $entityManager is an instance of \Doctrine\ORM\EntityManager.

$container = new ContainerBuilder();
$container->register('doctrine.orm.entitymanager', $entityManager);
var_dump($container->get('doctrine.orm.entitymanager'));

The component throws a ReflectionException if I try to resolve the instance. What is the correct approach to this?

PHP Fatal error:  Uncaught exception 'ReflectionException' with message 'Access to non-public constructor of class Doctrine\ORM\EntityManager' in /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php:955
Stack trace:
#0 /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php(955): ReflectionClass->newInstanceArgs(Array)
#1 /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php(488): Symfony\Component\DependencyInjection\ContainerBuilder->createService(Object(Symfony\Component\DependencyInjection\Definition), 'doctrine.orm.en...')
#2 /vagrant/sfs/test.php(12): Symfony\Component\DependencyInjection\ContainerBuilder->get('doctrine.orm.en...')
#3 {main}
  thrown in /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php on line 955

Fatal error: Uncaught exception 'ReflectionException' with message 'Access to non-public constructor of class Doctrine\ORM\EntityManager' in /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php:955
Stack trace:
#0 /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php(955): ReflectionClass->newInstanceArgs(Array)
#1 /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php(488): Symfony\Component\DependencyInjection\ContainerBuilder->createService(Object(Symfony\Component\DependencyInjection\Definition), 'doctrine.orm.en...')
#2 /vagrant/sfs/test.php(12): Symfony\Component\DependencyInjection\ContainerBuilder->get('doctrine.orm.en...')
#3 {main}
  thrown in /vagrant/sfs/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php on line 955

1 Answer 1

2

If you try to init the entity manager with the new command, this won't work.

new EntityManager();

You have to use the EntityManagerFactory. https://github.com/doctrine/DoctrineORMModule/blob/master/src/DoctrineORMModule/Service/EntityManagerFactory.php

But the EntityManager is in Symfony2 already registered as service

$container->get('doctrine.orm.entity_manager');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.