Autowire specific DBAL connection when using multiple of them

symfony get doctrine dbal connection
doctrine connections
doctrine too many connections
doctrine multiple mappings
doctrine mariadb
symfony entitymanagerinterface
manager in symfony
symfony database config

I'm using Doctrine 2 where I have multiple connections for DBAL. I have also multiple EntityManagers in ORM.

I need to be able to somehow autowire specific DBAL connection into other Symfony 3 services.

I can autowire any EntitiyManager using EntityManagerDecorator but don't know how to do the same with connection. I'm able to get the connection from EntityManager but I don't think it's the way to go.


You can specify wrapper class for doctrine connections, no proxy class needed:

#config.yml
doctrine:
    dbal:
        connections:
            default:
                wrapper_class: AppBundle\Connections\ConnectionDefault
                ...
            second:
                wrapper_class: AppBundle\Connections\ConnectionSecond
                ...

Connections should extend Doctrine\DBAL\Connection:

<?php

namespace AppBundle\Connection;

use Doctrine\DBAL\Connection;

class ConnectionDefault extends Connection
{

}

class ConnectionSecond extends Connection
{

}

and create service aliases:

#services.yml
services:
    ...
    AppBundle\Connections\ConnectionDefault: '@doctrine.dbal.default_connection'
    AppBundle\Connections\ConnectionSecond: '@doctrine.dbal.second_connection'

then you can simply inject desired connection into service by typehinting it:

class MyService {
    public function __construct(ConnectionDefault $connection) {...}
}

class MyOtherService {
    public function __construct(ConnectionSecond $connection) {...}
}

Symfony autowire DBAL connections – LPodolski, I'm using Doctrine 2 where I have multiple connections for DBAL. I have also multiple EntityManagers in ORM. I need to be able to somehow autowire specific​  git push current branch to specific remote server Ubuntu; Symfony autowire DBAL connections. 46235336/autowire-specific-dbal-connection-when-using-multiple-of


Each DBAL connection is always accessible in service container with the following identifier:

doctrine.dbal.[name]_connection

where [name] is the connection name

https://github.com/doctrine/DoctrineBundle/blob/master/Resources/doc/configuration.rst#doctrine-dbal-configuration

Multiple DBAL connections in Symfony 4 - Peter Lafferty, This article shows a couple of ways to configure connections and how to inject them with DBAL. In order to create a connection use the  2 Autowire specific DBAL connection when using multiple of them Sep 17 '17 1 Debian Docker config file Jan 21 '17 1 Pass multiple values using ajax and echo in another page Jan 24 '17


I had the same issue and tested it more precisely what works and why.

WORK FOR MANY DBAL CONNECTIONS USING PROXY

EDIT: I have done some proxy abstract class. Now each my connection class inherited from this proxy. It works fine :)

<?php

#src/AppBundle/Connections/ConnectionProxy.php

namespace AppBundle\Connections;

use Doctrine\DBAL\Connection;

abstract class ConnectionProxy
{
    private $connection;

    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
    }

    public function __call($method, $arguments)
    {
        if (is_callable(array($this->connection, $method))) {
            return call_user_func_array(array($this->connection, $method), $arguments);
        } else {
            return new \Exception("Call to undefined method '{$method}'");
        }
    }
}

My connection default class:

<?php

#src/AppBundle/Connections/ConnectionDefault.php

namespace AppBundle\Connections;

class ConnectionDefault extends ConnectionProxy
{

}

second connection:

<?php

namespace AppBundle\Connections;

class ConnectionSecond extends ConnectionProxy
{

}

Then I have added manually to my services file, for all my connections(2 connections) :

AppBundle\Connections\ConnectionDefault:
    public: true
    class: AppBundle\Connections\ConnectionDefault
    arguments: ['@doctrine.dbal.default_connection']

AppBundle\Connections\ConnectionSecond:
    public: true
    class: AppBundle\Connections\ConnectionSecond
    arguments: ['@doctrine.dbal.second_connection']

Then my connections are automatically autowiring when I use their types

class SaveEvent
{
    /** @var  Connection */
    private $connection;

    public function __construct(ConnectionDefault $connection)
    {
        $this->connection = $connection;
    }
    .....

INJECT CONNECTIONS FOR EACH SERVICE

The simplest option - but require create each service separately and inject connection name - it means manually wiring Arguments** in your services file(app/config/services.yml) e.g.

AppBundle\Classes\SaveEvent:
    public: true
    arguments:
      - '@doctrine.dbal.[connection_name]_connection'

where connection_name is your connection name. In below example we have two connections in config.yml and this value could be "default" or "database2":

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
              driver: pdo_sqlite
              charset: UTF8
              path: '%database_path%'
            database2:
              driver: pdo_sqlite
              charset: UTF8
              path: '%database_path%'

WORK FOR ONLY ONE DBAL CONNECTION

Autowiring for DBAL Connections works fine when we have only one connection. If I leave only default connection(remove connection database 2 from config.yml) and add some path to be autowired in app/config/services.yml:

AppBundle\Classes\:
    resource: '../../src/AppBundle/Classes'
    public: true

and if I use as type class Doctrine\DBAL\Connection in my Class, it means that I want inject default connection(@doctrine.dbal.default_connection) because I have the only one connection.

namespace AppBundle\Classes;

use Doctrine\DBAL\Connection;

class SaveEvent
{
    /** @var  Connection */
    private $connection;

    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
    }

// …
}

ANSWER

// Edited SimPod, the answer to your question is WORK FOR MANY DBAL CONNECTIONS USING PROXY or INJECT CONNECTIONS FOR EACH SERVICE as I described above.

I'm aware that you have managed with that issue but my answer can help the others.

Multiple connections causing "Circular reference detected for , I'm using Doctrine 2 where I have multiple connections for DBAL. I need to be able to somehow autowire specific DBAL connection into other I had the same issue and tested it more precisely what works and why. [Doctrine\ORM\ORMException] The EntityManager is closed. After I get a DBAL exception when inserting data, EntityManager closes and I'm not able to reconnect it. I tried like this but it didn't get a connection.


A simpler solution can be:

#services.yml
services:
    _defaults:
        bind:
            $dbSecond: '@doctrine.dbal.second_connection'

Use in controller or services:

public function my(Request $request, Connection $dbSecond)

Doctrine Event Listeners and Subscribers (Symfony 4.1 Docs), doctrine: dbal: default_connection: default connections: default: driver: pdo_mysql If I put it back to autowire: true with a single db connection, it works fine (as That's why I specified in the arguments and removed autowire. MySQL DBAL. MySQL extensions for Doctrine DBAL. Pharako\DBAL\Connection is an extension of Doctrine\DBAL\Connection—all functionality you get from the latter is also contained in the former, with a few add-ons specific to databases compatible with MySQL: multiple inserts; single and multiple upserts (update records if they exist, insert them


there is a new functionality in version 3.4 which gives the process much easier. See : Symfony 3.3 - Entity Manager injection into services with multiple database?

Guide to Spring @Autowired, For more, see The Event System on Doctrine's website. To register a service to act as an event listener or subscriber you have to tag it with the appropriate name​. specific DBAL connection and all the entity managers that use this connection​. In case you have multiple listeners for the same event you can control the  oci8: An Oracle driver that uses the oci8 PHP extension. sqlanywhere: A SAP Sybase SQL Anywhere driver that uses the sqlanywhere PHP extension. driverClass: Specifies a custom driver implementation if no 'driver' is specified. This allows the use of custom drivers that are not part of the Doctrine DBAL itself.


Questions for tag dbal, A guide to the most commonest usage of Springs @Autowired annotation and qualifiers. As an alternative, in Spring XML, it can be enabled by declaring it in To avoid this from happening, a bean can optional be specified as below: avoid ambiguity when Spring finds multiple beans of the same type. Handling multiple autowire candidates with @Primary is effective when only one primary candidate can be determined for multiple autowire candidates. When you need more control over the selection process, you can use spring @Qualifier annotation. The @Qualifier annotation allows you to associate a given name with a specific bean type. Thus


Inject the ManagerRegistry instead of the EntityManager, OroCommerce 4.1.2 UTF8MB4 Install error "1071 Specified key was too long; max key length Autowire specific DBAL connection when using multiple of them. For example, to limit autowire candidate status to any bean whose name ends with ‘Impl’, provide a value of ‘*Impl’. To provide multiple patterns, define them in a comma-separated list.


doctrine: dbal: default_connection: default connections: default: driver: When we ask it to find the entity manager for the since the CustomerManager can now be used in projects with multiple entity managers. What happens if you need exactly the EntityManager and not some specific repository? We will learn Spring Dependency Injection and Beans auto-wiring using Spring @Autowired annotation. @Autowired can be applied on a bean’s constructor, field, setter method or a config method to autowire the dependency using Spring’s dependency injection. @Autowired wires the dependency using bean datatype.