ant1j
ant1j

Reputation: 315

Can I define a new connection / Entity manager within a Bundle configuration?

I am trying to define a Bundle that helps to index some data (from an Entity) within a sqlite fts table, in order to do some full-text search.

I know I can define alternate connection and Entity Manager using the app config file (like shown in the cookbook), but I was wondering if it was possible to do so within the bundle configuration - by defining, for example, a service with the doctrine service injected, so that it can instantiate a new connection.

Sqlite is not mandatory, it can be any database type (supported by doctrine).

Thanks in advance for your help.

Upvotes: 0

Views: 1112

Answers (2)

room13
room13

Reputation: 1922

As far as i know it it not possible. Also database connections should be managed centralized in app/config.yml.

I would suggest to define the connection there and add a configuration option to you bundle where you can specify wich connection to use for the indexing stuff.

# app/config.yml
doctrine:
    dbal:
        default_connection:   default
        connections:
            default:
                dbname:               database
                host:                 localhost
                user:                 user
                password:             secret
                driver:               pdo_mysql
            my_data_index:
                dbname:               database
                driver:               pdo_sqlite

# configuration option of you bundle
acme_indexer:
    connection: my_data_index

Then in your bundle you can read this config option and obtain the connection/entity manager and inject it into your indexing service.

Edit:

You can for sure create a custom connection programaticly in your bundle

$params =   array(
    'driver'    =>'pdo_sqlite',
    ....
);

$factory = $container->get('doctrine.dbal.connection_factory')
$connection = $factory->createConnection($params);

But as i said before it is bad practice because you would evade the symfony standart way of defining database connections.

Upvotes: 0

MDrollette
MDrollette

Reputation: 6927

I would suggest looking at FOSUserBundle for how they handle multiple persistence layers (ORM, Couch, MongoDB). You would essentially be doing the same thing. It's just a matter of loading different configurations depending on the driver specified in app/config/config.yml.

Upvotes: 1

Related Questions