possible to assign db_name(Database.php) in a variable or any other option - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: possible to assign db_name(Database.php) in a variable or any other option (/showthread.php?tid=74310) |
possible to assign db_name(Database.php) in a variable or any other option - subodh6580 - 09-09-2019 Hello guys We want to connect one of too many databases as a default. as per user requriment so we want take a database name in a variable or Global variable. we are unable to use any variable in Database.php file so guys help me for this problem. RE: possible to assign db_name(Database.php) in a variable or any other option - InsiteFX - 09-09-2019 You can pass the database config group to the database method. So you would need to setup a config group for each database that you need to use. PHP Code: $db1 = \Config\Database::connect('group_one'); Note: Change the words “group_one” and “group_two” to the specific group names you are connecting to. SEE: Multiple Connections to Same Database RE: possible to assign db_name(Database.php) in a variable or any other option - kilishan - 09-09-2019 You can also pass the entire connection group to the connect method: PHP Code: $custom = [ One possible solution to make this a "global" is to create a new method in Config\Services.php that determines which is the correct database to connect to for this page view and return a shared instance of the database connection with the appropriate connection. Then, each time you call that method during that load it will return that same database connection. |