0

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.

Seb
  • 11
  • 3
  • is your database name really `bdd-erp`, because having a dash in the database name really seems odd. maybe only certain queries will add that? I'm out of other ideas, tbh. – Jakumi Jun 05 '20 at 07:28
  • Thanks. I tried with the name "bdd_erp" and still the same problem :(. – Seb Jun 05 '20 at 07:48

2 Answers2

1

I found the solution. Finally it is an anomaly of Symfony 5. I renamed the entity folder "ErpEntity" (instead of "EntityErp") and it works. Strange, but I can close this post.

Seb
  • 11
  • 3
  • I do not think that is an anomaly just not following the Symfony naming convention. Everything you do is YourNameController, YourNameRepository so it should follow the same with YourNameEntity. You most likely could have used Entity/Erp and it would have worked correctly as well. prefix: 'App\Entity\Erp' – Robert Saylor Jun 05 '20 at 10:12
  • I had tried before 'App\Entity\Erp', but still the bug. So I maked EntityErp then ErpEntity. – Seb Jun 05 '20 at 10:38
  • If in doubt, I did the test again with 'App\Entity\Erp', and always the bug. – Seb Jun 05 '20 at 10:44
  • Thanks for the update. You might want to open a bug report with Symfony. I have used 4 databases on Symfony 4 but not on Symfony 5 yet. The way I described works in Symfony 4. – Robert Saylor Jun 05 '20 at 10:45
  • Yes you are right, so I too at the beginning I put 'App\Entity\Erp' because it was the most obvious (and as explained in the documentation). But I really think there is an anomaly. I will test later with Symfony 5.2. – Seb Jun 05 '20 at 10:55
0

The connection starts with your entity manager so you would want to do it this way:

$em1 = $this->getDoctrine()->getManager();
$em2 = $this->getDoctrine()->getManager('erp');

$em1 would be your default first database, $em2 would be the second (erp)

Then construct your query this way:

$totos = $em2->getRepository(Toto::class)->findAll();
Robert Saylor
  • 1,279
  • 9
  • 11
  • Thank you for your reply. However, even with the following code, I still get the same error : `$emErp = $this->getDoctrine()->getManager('erp'); $totos = $emErp->getRepository(Toto::class)->findAll();` ---> Table 'bdd-erp.toto' doesn't exist – Seb Jun 04 '20 at 22:01