I create two connections for two databases as the Symfony recommends: https://symfony.com/doc/current/doctrine/multiple_entity_managers.html
I use Symfony 5.0.
I configure two entity managers:
# config/packages/doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
url: '%env(resolve:DATABASE_URL)%' #bdd-main
erp:
url: '%env(DATABASE_ERP_URL)%' #bdd-erp
orm:
auto_generate_proxy_classes: true
default_entity_manager: default
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: Main
erp:
connection: erp
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
Erp:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/EntityErp'
prefix: 'App\EntityErp'
alias: Erp
For the test I created a Toto object in EntityErp (so for database "bdd-erp").
When i try to save an object I can do it :
$erpEntityManager = $this->getDoctrine()->getManager('erp');
$toto = new Toto();
$toto->setName('Toto');
$erpEntityManager->persist($toto);
$erpEntityManager->flush();
In database "bdd-erp", the object is present.
But, when I just want to recover all the objects, it doesn't work :
$totos = $this->getDoctrine()
->getRepository(Toto::class, 'erp')
->findAll();
It is trying to connect to the wrong database, so I have this error :
An exception occurred while executing 'SELECT t0.id AS id_1, t0.name AS name_2 FROM Toto t0':
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'bdd-erp.toto' doesn't exist
Edit : I tried this, but still the same problem:
$emErp = $this->getDoctrine()->getManager('erp');
$totos = $emErp->getRepository(Toto::class)->findAll();
I don't understand, what am I doing wrong ?
==> EDIT - The solution : I found the solution. Finally it is an anomaly of Symfony 5. I renamed the entity folder "ErpEntity" (instead of "EntityErp") and it works (I had tried before 'App\Entity\Erp', but still the bug). Strange, but I can close this post.