我必须在我的应用程序中使用多个数据库连接。场景是:
dm_masterdb
保存数据库信息和用户凭据以登录应用dm_masterdb
,从数据库中获取登录用户的数据库信息,并根据其凭据建立连接。现在整个应用程序在这个新创建的数据库连接上运行,比如userDb
。现在我一直在做的是:
我创建了一个帮助连接到第二个数据库的以下帮助程序:
/**
* 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 );
}
}
在每个模型的构造函数中调用此函数connectDb()
以在访问数据库之前创建数据库连接。例如,我的一个模型如下:
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();
}
}
现在的问题是,
dm_masterdb
并全局访问用户数据库的连接,即无需在每个模型的构造函数中创建数据库连接?答案 0 :(得分:1)
您可以为此目的扩展CI_Model
。
首先在application/core
说MY_Model.php
下创建一个基本模型。前缀MY_
取决于配置文件中的变量$config['subclass_prefix'] = 'MY_';
。
MY_Model.php
class MY_Model extends CI_Model{
protected $myDB =null;
public function __construct(){
$this->connect_db();
}
public function connect_db(){
/***First fetch the configuration here from dm_masterdb
and assign it to the following array as needed ***/
//assign the values
$userDbConfig['hostname'] ='fetched value from above';
...................................
$userDbConfig['dbcollat'] = "utf8_general_ci";
$this->myDB = $this->load->database($userDbConfig, TRUE);
}
}
现在在您的模型文件夹下,假设您有一个名为test_model的模型
test_model.php
class Test_model extends MY_Model
{
function __construct()
{
parent::__construct();
}
public function get_result(){
$t = $this->db->query('query on default db')->result_array();
$t1 = $this->myDB->query('query on your dynamic db')->result_array();
echo "<pre>";
print_r($t1);
print_r($t);
}
}
希望这可以帮助你...
答案 1 :(得分:1)
在您的database.php中,您有默认配置,如
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
..............
..............
复制此配置并将default
索引更改为default
之外的其他内容,如
$db['second_db']['hostname'] = 'localhost';
$db['second_db']['username'] = 'root';
$db['second_db']['password'] = '';
..............
..............
使用第二个db
$secondDb = $this->load->database('second_db', TRUE);
然后您将使用$secondDb->foo()
代替$this->db->foo()
第二种方法是手动连接数据库,如
$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$this->load->database($config);
有关详细信息,请参阅此处http://ellislab.com/codeigniter/user-guide/database/connecting.html
答案 2 :(得分:0)
很快我意识到,所有这两个连接的做法都是放慢我的应用程序。
最后我去了:
不是创建第二个连接,而是在我的查询中使用dbname
。tableName
访问第二个数据库,相对来说应用程序效率更高。