使用hmvc codeigniter中的钩子在多个数据库之间切换

时间:2018-07-11 11:43:05

标签: php codeigniter doctrine-orm hmvc-codeigniter

我正在使用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 变量。我为控制器定义了此变量。但没有运气..请帮助我解决这个问题。

0 个答案:

没有答案