0

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

Nearyuk
  • 167
  • 2
  • 12
  • 1
    have you tried this https://stackoverflow.com/questions/51556454/dependency-inject-non-default-entity-manager-into-service-in-symfony ? – Manzolo Aug 30 '19 at 15:19
  • 3
    The trick here is that your $em inside your repository works with one database with exact default connection (I suppose). You need to create a second one entity manager that would use a second connection to a second database and then inject it inside your repository constructor. Then you could make a query to the second database with this entity manager. – Vasiliy Toporov Aug 31 '19 at 06:50
  • 1
    Vasily is right, think that an entity manager is a connection to a database. If you have 4 databases you should configure 4 entity manager with the linked documentation. – Smaïne Sep 01 '19 at 21:21
  • Thanks for your answer, i will try that and let you know if it worked ! – Nearyuk Sep 02 '19 at 09:09

1 Answers1

1

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'
Nearyuk
  • 167
  • 2
  • 12
  • Did query worked for you? I followed same as yours answer. But execute returns true only not my query data. – nas Aug 01 '20 at 17:43