Using multiple databases in Symfony2


Time change everything. When i was started learning php i never  heard the name  Symfony. But today i love this framework. Now i will tell you how we can configure and use  two databases in Symfony2 .

We have to do the following Steps:-

1.Open the config.yml (Symfony->app->config->config.yml)

imports:
    - { resource: parameters.ini }
    - { resource: security.yml }

framework:
    secret:          %secret%
    charset:         UTF-8
    router:          { resource: "%kernel.root_dir%/config/routing.yml" }
    form:            true
    csrf_protection: true
    validation:      { enable_annotations: true }
    templating:      { engines: ['twig'] } #assets_version: SomeVersionScheme
    session:
        default_locale: %locale%
        lifetime:       600
        auto_start:     true

# Twig Configuration
twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%
    extensions:
     - twig.extension.debug
     - twig.extension.text

# Assetic Configuration
assetic:
    debug:          %kernel.debug%
    use_controller: false
    filters:
        cssrewrite: ~
        # closure:
        #     jar: %kernel.root_dir%/java/compiler.jar
        # yui_css:
        #     jar: %kernel.root_dir%/java/yuicompressor-2.4.2.jar

# Doctrine Configuration
doctrine:
    dbal:
        default_connection:       default
        connections:
            default:
                dbname:           devdream
                user:             aman
                password:         root
                host:             192.168.2.102
            forum:
                dbname:           dev
                user:             aman
                password:         root
                host:             192.168.2.102
    orm:
        default_entity_manager:   default
        entity_managers:
            default:
                connection:       default
                mappings:
                    DevDreamBundle: ~
                metadata_cache_driver: apc
                query_cache_driver: apc
                result_cache_driver: apc
            forum:
                connection:       forum
                mappings:
                    DevDreamBundle: ~
# Swiftmailer Configuration
swiftmailer:
    transport: %mailer_transport%
    host:      %mailer_host%
    username:  %mailer_user%
    password:  %mailer_password%

jms_security_extra:
    secure_controllers:  true
    secure_all_services: false

i have modified  the connection part and write the names of both the databases.

This is the configuration part

2. In the second step, i will tell you , how to use the second database in controller

So this is the following code for access second database in Controller

$query=   $this->get('doctrine')->getEntityManager('forum')
                        ->createQuery("SELECT th FROM DevDreamBundle:devdreamUser th")
                        ->setMaxResults(4)
                        ->getResult();

You are good to go……

Advertisements

8 thoughts on “Using multiple databases in Symfony2

  1. Hello,
    if you know, is possible to build in symfony2 an query using several database
    for ex: is need to join an table from DB1 with and table from DB2
    Thanks

    Reply
  2. Thanks Aman. Really helpful. You saved my time.. 🙂
    I have one concern that how can I connect to oracle database using Symfony? I googled for it, but not much help.

    Reply
  3. i am trying to acccess the forum DB using $this->get(‘doctrine’)->getEntityManager(‘forum’) on my custom repository. But its still trying to access the table from default database

    Reply
    • check ur orm mapping in config.yml or alternate way is

      make database connection by
      $defaultConn2 = $this->get(‘doctrine.dbal.forum_connection’);
      here forum is your database name
      $result=$defaultConn2->fetchAll(‘Select * from table ‘);

      srry for your late reply

      Reply

      Reply
  4. Thanks a lot for the write up! One thing isn’t clear to me. In the config file, you set the mapping such that both of the entity managers are assigned to the same bundle:

    mappings:
    DevDreamBundle: ~

    But then in the controller, you explicity say that you want to use the forum entity manager like so:

    $this->get(‘doctrine’)->getEntityManager(‘forum’)

    What’s the point in setting the entity manager->bundle mapping in the config file if you still have to explicitly request the correct entity manager in the controller? Perhaps if you didn’t explicity request the forum manager and just entered:

    $this->get(‘doctrine’)->getEntityManager()

    due to the mapping in the config file, you would get the forum entity manager as default? I am trying to understand this. Thanks a lot!

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s