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……