问题
I have recently added a new database to my Symfony 3.3 application. Then my services with injected entity manager are no longer working and return the following error:
Cannot autowire service "RouterBundle\Utils\RoutersUtils": argument "$em" of method "__construct()" references class "Doctrine\ORM\EntityManager" but no such service exists. Try changing the type-hint to one of its parents: interface "Doctrine\ORM\EntityManagerInterface", or interface "Doctrine\Common\Persistence\ObjectManager".
So my config.yml file is now like :
# Doctrine Configuration
doctrine:
dbal:
default_connection: router
connections:
router:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_router_name%'
user: '%database_router_user%'
password: '%database_router_password%'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
row_format: DYNAMIC
app:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_app_name%'
user: '%database_app_user%'
password: '%database_app_password%'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
orm:
default_entity_manager: router
entity_managers:
router:
connection: router
mappings:
RouterBundle: ~
dql:
string_functions:
# Match agains should have the path to the Sha1 class created in the previous step
SHA1: RouterBundle\Extensions\Doctrine\Sha1
app:
connection: app
mappings:
AppBundle: ~
One of my service like :
<?php
namespace RouterBundle\Utils;
use Doctrine\ORM\EntityManager;
class RoutersUtils
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
...
and my services.yml ( I tried to add this arguments but doesn't work)
...
RouterBundle\Utils:
public: true
arguments:
- '@doctrine.dbal.router_connection'
...
Any idea how to inject the correct Entity Manager? Can I also inject entity manager by default using the "default_entity_manager: router" parameters in teh config file ?
Thanks for the help !
Pierre
回答1:
You cannot autowiring by using the EntityManagerInterface
if you have many managers. You will have to pick the right manager in your service definition for each service.
RouterBundle\Utils:
$em: '@your_entity_manager_service'
PS : You should consider upgrading to sf3.4 as 3.3 is not maintenaited anymore. It also comes with local binding, which will be really useful for what you want to do.
See: https://symfony.com/blog/new-in-symfony-3-4-local-service-binding
来源:https://stackoverflow.com/questions/49195929/symfony-3-3-entity-manager-injection-into-services-with-multiple-database