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
Thanks for making this post, I’m currently following the steps. No problems so far 😀
It may also be useful for other to see the doctrine yml reference:
http://symfony.com/doc/current/reference/configuration/doctrine.html
Thanks a lot Aman: you saved the day.
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
i think so …
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.
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
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
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!