Search code examples
symfonydoctrinesymfony4entitymanager

Symfony 4 EntityManager multiple database


I would like to execute SQL Request with multiple database inside my APP\Repository file.

I found this, but it's only work with APP\Controller file.

Symfony multiple Entity Managers and Connections

But unable to do $this->getDoctrine()->getManager('database1') inside a Repository file

Here is my code :

App\Repository

public function __construct(EntityManagerInterface $em)
{

    $this->em = $em;
}

/**
 * @param string $code
 * @return mixed[]
 * @throws DBALException
 */
function Getdata(string $code) {

    // I would be able to execute $sql with my DATABASE2
    $sql = "SELECT * FROM api";
    $conn = $this->em->getConnection();
    $query = $conn->prepare($sql);
    $query->execute();
    $result = $query->fetchAll();
    return($result);
}

/config/services.yaml

doctrine:
dbal:
    default_connection: default
    connections:
        default:
            # configure these for your database server
            url: '%env(DATABASE1)%'
            driver: 'pdo_mysql'
            server_version: '5.7'
            charset: utf8mb4
        customer:
            # configure these for your database server
            url: '%env(DATABASE2)%'
            driver: 'pdo_mysql'
            server_version: '5.7'
            charset: utf8mb4

Thanks by advance for the answer, if i'm not clear, don't hesitate to ask


Solution

  • Here is what i implemented, could help :

    App\Repository

    public function __construct(ManagerRegistry $mr)
    {
        $this->mr = $mr;
    }
    
    function Getdata() {
            $em = $this->mr->getManager("db1");
            $sql = "SELECT * FROM table"
            $conn = $em->getConnection();
            $query = $conn->prepare($sql);
            $query->execute();
            return ($query);
    }
    

    /config/doctrine.yaml

    doctrine:
        dbal:
            default_connection:       db1
            connections:
                db1:
                    url: '%env(DB1)%'
                    driver: pdo_mysql
                    server_version: 'mariadb-10.4.7'
    
                db2:
                    url: '%env(DB2)%'
                    driver: pdo_mysql
                    server_version: 'mariadb-10.4.7'
        orm:
            auto_generate_proxy_classes: true
            entity_managers:
                db1:
                    connection: db1
                    mappings:
                        App:
                            is_bundle: false
                            type: annotation
                            dir: '%kernel.project_dir%/src/Entity'
                            prefix: 'App\Entity'
                db2:
                    connection: db2
                    mappings:
                        Customer:
                            is_bundle: false
                            type: annotation
                            dir: '%kernel.project_dir%/src/Entity/'
                            prefix: 'App\Entity'