CodeIgniter Forums
Dynamic Database - 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: Dynamic Database (/showthread.php?tid=76326)



Dynamic Database - wesleysiqueira - 05-03-2020

When i used ci3 i used like this:
Code:
$ci =& get_instance();

and then
Code:
$ci->session->userdata();


to clarify i wold like to load databases dynamically based on my encrypted session.

Rigth now i have : 
Code:
public $default = [
'DSN'      => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => '', // I WOULD LIKE TO CHANGE THIS DYNAMICALLY BUT IN CI4 I DON'T KNOW HOW TO DO. LIKE: $THIS->SESSION->GET('COMPANY');
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug'  => (ENVIRONMENT !== 'production'),
'cacheOn'  => false,
'cacheDir' => '',
'charset'  => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre'  => '',
'encrypt'  => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port'     => 3306,
];



But now with ci4 i don't know how to do. Anyone can help me? Smile


RE: Dynamic Database - motownphilippe - 02-27-2021

I think you can dynamically change the database from your model or other class with
$db->setDatabase($database2_name);

See https://codeigniter.com/user_guide/database/connecting.html