Pregunta

I know that using the Doctrinebundle in Symfony2 it is possible to instantiate multiple DB connections under Doctrine...

$connectionFactory = $this->container->get('doctrine.dbal.connection_factory');
$connection = $connectionFactory->createConnection(array(
    'driver'   => 'pdo_mysql',
    'user'     => 'foo_user',
    'password' => 'foo_pass',
    'host'     => 'foo_host',
    'dbname'   => 'foo_db',
));

I'm curious if this is the case if you are using PURELY Doctrine though?, I've set up Doctrine via Composer like so...

{
    "config": {
        "vendor-dir": "lib/"
    },
    "require": {
        "doctrine/orm": "2.3.4",
        "doctrine/dbal": "2.3.4"
    }
}

And have been looking for my ConnectionFactory class but am not seeing it anywhere? Am I required to use Symfony2 to do this?

Should I just download ConnectionFactory.php from the DoctrineBundle and include it in my DBAL folder?? idk?

¿Fue útil?

Solución

A bundle is only in the context of symfony needed, it wraps the orm into symfony infrastructure (services, etc.). For pure use of the orm you should read the ORM: Installation and Configuration. As you see you must create an entity manager by yourself with EntityManager::create($dbParams, $config), so simply create different entity managers for your different databases.

For DBAL use you should read DBAL: Configuration and see, a connection can simply obtained trough DriverManager::getConnection($connectionParams, $config); But if you are sure the ConnectionFactory has no dependency to symfony stuff and you really need it, you can try copy it to your code and construct a new factory to obtain a DBAL connection.

$connectionFactory = new ConnectionFactory(array());
$connection = $connectionFactory->createConnection(array(
    'driver'   => 'pdo_mysql',
    'user'     => 'foo_user',
    'password' => 'foo_pass',
    'host'     => 'foo_host',
    'dbname'   => 'foo_db',
));

But take care, this is a DBAL connection, i.e. it's a abstraction layer which sits on top of PDO and only for pure SQL queries. If you need a entity manager you have to initialize it as mentioned in the docs above, or maybe you find another entity manager factory class, which you can "copy".

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top