Multiple database connection in cakephp 3

Jigar Dhaduk picture Jigar Dhaduk · May 1, 2015 · Viewed 12.3k times · Source

I am trying to connect to multiple databases in cakephp 3. I have tried many times but all questions and answers are in cakephp 2. I found Configuring Connections at cakephp 3 documentation. But I can't understand it.

I have two databases :

1. tracking_system
     (Tables: trackers, events, etc..)
2. tracking_system_2
     (Tables: todos, actions, etc..)

Working with database tracking_system is completely running. But I don't know, how to connect to multiple databases (in my case, with second database tracking_system2).

Thanks in advance for help.

Answer

José Lorenzo Rodríguez picture José Lorenzo Rodríguez · May 3, 2015

You can declare the defaultConnectionName() method on the tables that will use by default another connection. In any of your Table classes:

public static function defaultConnectionName()  
{
    return 'another_config_name';
}