0

I am working with CI3 and I've never ever use hooks before. I have 2 user panel,

  1. Admin (one db)
  2. 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.

Saclt7
  • 377
  • 1
  • 8
  • 32
surma
  • 97
  • 1
  • 9
  • At the risk of sounding dumb I'm going to ask anyway. Why does each user have their own DB versus a user table that can be a foreign key? – camdixon Jul 11 '18 at 11:48
  • each user has their own company info to deal with and its project requirements – surma Jul 11 '18 at 11:55
  • each user has their own site to deal with that db – surma Jul 11 '18 at 11:56
  • Not sure if I understand you correctly but set a default `DB_TYPE` in your root index.php, same place as the ENVIRONMENT constant is defined. Then when the user logs in, store their login level in the session and update your function to read a `DB_TYPE` variable from the session? – Simon K Jul 11 '18 at 12:14

0 Answers0