I have a table that contains all database name. I want to create a run time database connection in CakePHP and search data in the database. Is there any way in CakePHP to fetch database name from default database and create runtime connection?
Asked
Active
Viewed 75 times
0
-
Don't know if this helps $this->db = & ConnectionManager::getDataSource('default'); in controller and then use this connection – Keyur Padalia Aug 24 '17 at 10:51
-
Hi Keyur, thank you but i have a table that contain all database name . condition is that i want to search something .if it is found in first database than no need of second connection otherwise need to create connection with second database. – Sourabh Devera Aug 24 '17 at 10:57
-
have a look at this question https://stackoverflow.com/questions/30262176/dynamically-change-database-connection-in-cakephp-3 – Keyur Padalia Aug 24 '17 at 10:58
-
this is fine but not great – Sourabh Devera Aug 24 '17 at 11:45
-
If you have a finite number of databases then I think you could create that many connections in app/config.php and then use them on fly as needed – Keyur Padalia Aug 24 '17 at 11:47
-
How to use it. i use on fly but in my model it use default datasource what to do – Sourabh Devera Aug 29 '17 at 13:53
-
Try $this->db = & ConnectionManager::getDataSource(YOUR_DATABASE_NAME); – Keyur Padalia Aug 29 '17 at 13:55