0

我正在使用 CI3,我以前从未使用过钩子。我有 2 个用户面板,

  1. 管理员(一分贝)
  2. 用户(每个用户都有自己的数据库)

我的钩子中有一个名为 switchDatabase 的类,我在其中添加了以下代码以基于管理员和用户在多个数据库之间切换。

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);
    }
}

不,我的问题是,我应该如何或在哪里定义DB_TYPE变量。我将此变量定义为我的控制器。但没有运气..请帮我解决这个问题。

4

0 回答 0