2

Every tutorial or documentation regarding this subject is used with sql queries but never with new orm. I have two data sources defined in config/app.php: default and not_default with same tables and columns, when the user login the default database is connected. when the user clicks a certain button i need to change the connected database to not_default:

I achieved this so far:

ConnectionManager::get('default')->disconnect();
ConnectionManager::get('not_default')->connect();
ConnectionManager::get('not_default')->begin();  //dont know if required

when doing this: echo ConnectionManager::get('not_default')->isConnected(); I get 1 for not_default and 0 for default so it is connected but the problem is when getting the model's TableRegistry the connectionname is still the default one!! How to change it???

I want to be able to get data using orm from database other than the default one.

ndm
  • 59,784
  • 9
  • 71
  • 110

1 Answers1

0

You can use ConnectionManager::alias().

Connection aliases allow you to rename active connections without overwriting the aliased connection.

ConnectionManager::alias('not_default','default');
//from now on, all calls to default will resolve to not_default datasource