duanlang1196 2013-12-29 08:11
浏览 33
已采纳

Codeigniter - 在哪里创建第二个数据库连接?

I have to use multiple database connections in my application. The scenario is:

  • I have a dm_masterdb that holds the database information and the user credentials to login to the app
  • After logging into the app, this dm_masterdb is no more needed, the database information of the logged in user is taken from the database and a connection is made according to his credentials. And now whole application operates on this newly created database connection, say userDb.

Now what I have been doing is:

I have created a the following helper that aids in connecting to the second database:

/**
 * Aids in connecting to the passed database
 * @param  string $db_name database name to which the connection is required
 * @return object          Database object for the connection made
 */
function connectDb($db_name) {
    // Get current Codeigniter instance
    $CI =& get_instance();

    try {

        $userDbConfig['hostname'] = $CI->db->hostname;
        $userDbConfig['username'] = $CI->db->username;
        $userDbConfig['password'] = $CI->db->password;
        $userDbConfig['database'] = $db_name;
        $userDbConfig['dbdriver'] = "mysqli";
        $userDbConfig['dbprefix'] = "";
        $userDbConfig['pconnect'] = FALSE;
        $userDbConfig['db_debug'] = TRUE;
        $userDbConfig['cache_on'] = FALSE;
        $userDbConfig['cachedir'] = "";
        $userDbConfig['char_set'] = "utf8";
        $userDbConfig['dbcollat'] = "utf8_general_ci";

        $userDb = $CI->load->database($userDbConfig, true);

        return $userDb;

    } catch (Exception $e) {

        $error = 'The error thrown is: ' . $e->getMessage();
        $error  .=  'Error thrown while database connection to ' . $db_name;

        show_error($error, 500);
        log_message( 'error', $error );
    }
}

This function connectDb() is called in the constructor of each and every model to create a database connection before accessing the database. For example one of my models is given below:

class Payments extends CI_Model {

    private $userDb;

    public function __construct()
    {
        parent::__construct();
        $this->userDb = connectDb($this->session->userdata('db_name'));
    }

    public function fetchChartData($period, $type)
    {
        //...
        $result = $this->userDb->query($query);
        return $result->result_array();
    }
}

Now the question is,

  1. Is this the right way that I am doing it?
  2. Is there any way that I can make it more efficient?
  3. Would it be possible that I can drop off the existing database connection to dm_masterdb and access this connection to user's database globally i.e. without having to create database connection in each and every model's constructor?
  • 写回答

3条回答 默认 最新

  • douao2000 2014-01-07 06:33
    关注

    Soon I realized that, all that making two connections did was to slow down my app.

    Finally I went with: Not create the second connection but to access the second database using it's dbname.tableName in my queries and the app was more efficient comparatively.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失