1

We are trying to create a bundle that should run a few queries to setup data in another database. We want to do this by creating a bundle with a separate doctrine connection. The problem is that we don't have the entities we are trying to update, so it's not possible to obtain the repository. Is it possible get a query builder by doctrine connection name?

doctrine.yaml

when@behat:
    doctrine:
        dbal:
            connections:
                bundle_connection:
                    host: '%env(MYSQL_AUTHENTICATOR_HOST)%'
                    port: '%env(MYSQL_AUTHENTICATOR_PORT)%'
                    user: '%env(MYSQL_AUTHENTICATOR_USERNAME)%'
                    password: '%env(MYSQL_AUTHENTICATOR_PASSWORD)%'
                    dbname: 'authenticator_dev'
                        mapping_types:
                        enum: string
                        bit: boolean
                    server_version: 8.0.2-dmrbin

UpdateDatabase.php

<?php declare(strict_types=1);

namespace Bundle\Authenticator\Behat;

use Behat\Behat\Context\Context;
use Symfony\Bridge\Doctrine\ManagerRegistry;

class AuthenticationUserContext implements Context
{
    private ManagerRegistry $managerRegistry;

    public function __construct(ManagerRegistry $managerRegistry)
    {
        $this->managerRegistry = $managerRegistry;
        $conn = $this->managerRegistry->getConnection('bundle_connection');
        
        //run queries with this connection
    }
}

Other suggestions are welcome. We are thinking of duplicating the entities into this bundle but that would mean maintaining the same class in 2 projects.

Jeroen de Beer
  • 340
  • 4
  • 15
  • Just to clarify, are you trying to get just the DBAL connection or do you really want an entity manager? I think you just want the connection. [This answer](https://stackoverflow.com/questions/51556454/dependency-inject-non-default-entity-manager-into-service-in-symfony/51558092#51558092) shows how you can autowire a DBAL connection and inject it. – Cerad Jul 13 '22 at 12:15

0 Answers0