Is it possible to autowire a doctrine connection by name and run queries

Viewed 42

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.

0 Answers
Related