I am working with CI3 and I've never ever use hooks before. I have 2 user panel,
- Admin (one db)
- Users (each user has their own db)
I have a class called switchDatabase in my hooks where I added the following code to switch between multiple database based on admin and users.
function switchDatabase()
{
$CI = &get_instance();
$CI->load->model("common");
if (DB_TYPE != 'default') {
$rts = $CI->doctrine->em->getRepository("entities\\AppCompany")
->findOneBy(array("companyName" => DB_TYPE));
$data = array();
$data['dbHost'] = $rts->getHost();
$data['dbUsername'] = $rts->getUsername();
$data['dbPassword'] = $rts->getPassword();
$data['dbDatabase'] = $rts->getDatabase();
$CI->common->initialize($data, true);
$config = array();
$config['hostname'] = $data['dbHost'];
$config['username'] = $data['dbUsername'];
$config['password'] = $data['dbPassword'];
$config['database'] = $data['dbDatabase'];
$config['dbdriver'] = "mysqli";
$config['dbprefix'] = "";
$config['pconnect'] = TRUE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$config['swap_pre'] = '';
$config['autoinit'] = TRUE;
$config['stricton'] = FALSE;
$CI->load->database($config);
define('FOREIGN_DB', true);
}
}
No my problem is, how or where shall I define DB_TYPE variable. I defined this variable to my controller. but no luck..Please help me to solve this issue.