How to change the database on the fly throughout the website


hi,I want the user to press a button and specify which database to use. My issue is that I can change the database easy enough as below but I dont know how to set this new database to every controller. In the appcontroller would be the best place but i dont know how to pass the appcontroller a variable to set the database?

$connection = ConnectionManager::get(‘db2’); // ‘db2’ where my second database is configured
ConnectionManager::alias(‘db2’, ‘default’);


You could potentially store the database name in the session, which would make it available in AppController or your controller’s beforeFilter action. This might not be the most advisable approach though.