How to setup multiple database connection using doctrine on Expressive?

Hi, I’ve been trying to make a restful api using Expressive. I need a multiple database connection on my project. And I tried Doctrine, but it seems that Doctrine only support single database connection?
This is my doctrine configuration:

use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;

return [
    'doctrine' => [
        'connection' => [
            'orm_default' => [
                'params' => [
                    'url' => 'pgsql://postgres:postgres@localhost/db1'
                ]
            ],
            'orm_db2' => [
                'params' => [
                    'url' => 'pgsql://postgres:postgres@localhost/db2'
                ]
            ],
            'orm_db3' => [
                'params' => [
                    'url' => 'pgsql://postgres:postgres@localhost/db3'
                ]
            ]
        ],
        'driver' => [
            'orm_default' => [
                'class' => MappingDriverChain::class,
                'drivers' => [
                    'App\Entity' => 'my_entity',
                ]
            ],
            'orm_db2' => [
                'class' => MappingDriverChain::class,
                'drivers' => [
                    'App\Entity' => 'my_entity',
                ]
            ],
            'orm_db3' => [
                'class' => MappingDriverChain::class,
                'drivers' => [
                    'App\Entity\DB3' => 'db3_entity',
                ],
            ],
            'my_entity' => [
                'class' => AnnotationDriver::class,
                'cache' => 'array',
                'paths' => __DIR__ . '/../../src/App/Entity'
            ],
            'db3_entity' => [
                'class' => AnnotationDriver::class,
                'cache' => 'array',
                'paths' => __DIR__ . '/../../src/App/Entity/DB3'
            ]
        ],
    ]
];

And this is my factories:

/*...*/
'doctrine.entity_manager.orm_default' => \ContainerInteropDoctrine\EntityManagerFactory::class,
            'doctrine.entity_manager.orm_db2' => \ContainerInteropDoctrine\EntityManagerFactory::class,
            'doctrine.entity_manager.orm_db3' => \ContainerInteropDoctrine\EntityManagerFactory::class,
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
        $db1Em = $container->get('doctrine.entity_manager.orm_default'); //works
        $db2Em = $container->get('doctrine.entity_manager.orm_db2'); // doesn't return db
        $db3Em = $container->get('doctrine.entity_manager.orm_db3'); // doesn't return db
        return new ApiInquiryAction($db1Em, $db2Em, $db3Em);
    } 

When I called it using container, only the ‘doctrine.entity_manager.orm.default’ that works. ‘orm_db2’ and ‘orm_db3’ doesn’t return entity manager. Did I not configured it correctly or doctrine doesn’t support multiple database connection?

Looks like you are missing the $configKey. Have a look at this example: https://github.com/DASPRiD/container-interop-doctrine#configuration

return [
    'dependencies' => [
        'factories' => [
            'doctrine.entity_manager.orm_default' => \ContainerInteropDoctrine\EntityManagerFactory::class,
            'doctrine.entity_manager.orm_other' => [\ContainerInteropDoctrine\EntityManagerFactory::class, 'orm_other'],
        ],
    ],
];
2 Likes

Thanks for the answers. It works now. :grin:

Whoa, what’s this dependency on ContainerInteropDoctrine?

I thought doctrine-module supported this already: https://github.com/doctrine/DoctrineORMModule/blob/master/docs/configuration.md#how-to-use-two-connections

(though the single-connection code example doesn’t seem to support that heading text).

@Greg_Bell ContainerInteropDoctrine is a PSR container provider that can be used anywhere while orm module is a zend-mvc module and requires mvc application bootstrap to be properly configured.