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

4
  • 1
    have you tried this stackoverflow.com/questions/51556454/… ? Commented Aug 30, 2019 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. Commented Aug 31, 2019 at 6: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. Commented Sep 1, 2019 at 21:21
  • Thanks for your answer, i will try that and let you know if it worked ! Commented Sep 2, 2019 at 9:09

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

1 Comment

Did query worked for you? I followed same as yours answer. But execute returns true only not my query data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.