如何在codeigniter中切换数据库 [英] how to switch database in codeigniter

查看:252
本文介绍了如何在codeigniter中切换数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CI的新手,我只是想知道是否有一种方法来切换CI中的数据库,例如:
在登录页面中,有一个下拉列表,您可以指定要连接的数据库,查看看起来像这样

 < select name =selectid =select> 
< option value =1> DB1< / option>
< option value =2> DB2< / option>
< / select>

在database.php中有两个数据库,一个是 default 另一个是 local
默认情况下我使用默认的。



$ active_group ='default';
$ active_record = TRUE;



我的问题是我如何根据下拉值指定db, ,连接到默认值,DB2转到本地一个....
我知道如何手动swtich db:

  $ this-> load-> database('default',TRUE); OR 
$ this-> load-> database('local',TRUE);

由于我有不同的控制器和模型,我怎么可以实现这...我试过像:
首先获取登录函数中dropdownlist的值:

  $ this-> load-& My_Model'); 
$ db = $ this-> input-> post('select')
$ this-> My_Model-> getDB($ db);

然后在我的模型中我得到函数:

  function getDB($ db)
{
if($ db == 1)
{
$ this-> db = $ this-> load-> database('default',TRUE);
}
elseif($ db == 2)
{
$ this-> db = $ this-> load-> database ;
}
}

不幸的是,它不工作.... 。任何帮助将非常感激!!!!

解决方案

我得到它的工作。首先我编辑我的database.php配置文件以包括新的数据库。然后,在我的模型中,我声明了一个类变量 $ current_db ,我写了以下两个函数:

  function getDB($ db){
if($ db == 1){
$ this-> current_db = $ this-> load-> database ('default',TRUE);
} elseif($ db == 2){
$ this-> current_db = $ this-> load-> database('local',TRUE);
}
}

function dyno_query($ table,$ id){
$ query = $ this-> current_db-> get_where ('id'=> $ id));
return $ query-> result_array();
}

然后在我的控制器中,我运行以下命令:

  $ arr = array(); 
$ this-> My_Model-> getDB(1);
$ arr ['default'] = $ this-> My_Model-> dyno_query('default_table',2);
$ this-> My_Model-> getDB(2);
$ arr ['local'] = $ this-> My_Model-> dyno_query('local_table',74);
var_dump($ arr);

结果是一个包含两个数据库数据的数组。也许这一切的关键是定义一个类变量不是$ db。



EDIT:



为了保持每个页面加载当前数据库,您需要使用会话。在处理下拉列表数据的控制器函数中,您可以输入类似于以下内容的内容:

  $ db = $ this-> ; input-> post('select')
$ this-> My_Model-> getDB($ db);
$ this-> session-> set_userdata($ db);

在将使用数据库的任何控制器中,您需要添加代码以加载当前数据库:

  function __construct(){
parent :: __ construct
$ this-> load-> model('My_Model');
$ db = $ this-> session-> userdata('db');
$ this-> My_Model-> getDB($ db);
}

EDIT: b

如果您需要从各种模型访问相同的数据库,我建议使用 mddd在EllisLab 。只需使用下面的代码创建一个名为Db_manager.php的PHP文件,并将其放到应用程序/ libraries目录中:

  class Db_manager 
{
var $ connections = array();
var $ CI;

function __construct()
{
$ this-> CI =& get_instance();
}

函数get_connection($ db_name)
{
//连接存在?返回
if(isset($ this-> connections [$ db_name]))
{
return $ this-> connections [$ db_name];
}
else
{
//创建连接。把它返还。
$ this-> connections [$ db_name] = $ this-> CI-> load-> database($ db_name,true);
return $ this-> connections [$ db_name];
}
}
}

strong> model 将使用数据库添加以下内容:

  var $ current_db; 

function __construct(){
parent :: __ construct();
$ this-> load-> library('Db_manager');
$ db = $ this-> session-> userdata('db');
if($ db == 1){
$ this-> current_db = $ this-> db_manager-> get_connection('default');
} else {
$ this-> current_db = $ this-> db_manager-> get_connection('alternate');
}
}


i'm new to CI and i just wanna know is there a way to switch databases in CI,Eg: In login Page, there's a dropdown list that you can specify which database you wanna connect with, view look like this

<select name="select" id="select"  >
  <option value="1">DB1</option>                
  <option value="2">DB2</option>              
</select>

And i got 2 databases in database.php one is defaultthe other is local by default i'm using the default one.

$active_group = 'default'; $active_record = TRUE;

My question is how do i specify db based on the dropdown value, say if i pick DB1, connect to default one, DB2 goes to local one.... I know how to swtich db manually by :

 $this->load->database('default', TRUE); OR
 $this->load->database('local', TRUE);

Since i got different controller and model, how can i achieve this... i did try like: first get the value of dropdownlist in login function:

$this->load->model('My_Model');
$db = $this->input->post('select')
$this->My_Model->getDB($db);

and then in my model i got function :

function getDB($db)
{
    if($db ==1)
    {
        $this->db  = $this->load->database('default', TRUE);
         }
    elseif($db ==2)
    {
        $this->db  = $this->load->database('local', TRUE);
    }
}

Unfortunately, it doesn't work..... Any help would be much Appreciated!!!!

解决方案

I got it to work. First I edited my database.php configuration file to include the new database. Then, in my model, I declared a class variable $current_db, and I wrote the following two functions:

function getDB($db){
    if($db ==1){
        $this->current_db  = $this->load->database('default', TRUE);
    } elseif($db ==2) {
        $this->current_db  = $this->load->database('local', TRUE);
    }
}

function dyno_query($table, $id){
    $query = $this->current_db->get_where($table, array('id' => $id));
    return $query->result_array();
}

Then in my controller, I ran the following:

$arr = array();
$this->My_Model->getDB(1);
$arr['default'] = $this->My_Model->dyno_query('default_table', 2);
$this->My_Model->getDB(2);
$arr['local'] = $this->My_Model->dyno_query('local_table', 74);
var_dump($arr);

The result was an array with data from both databases. Maybe the key to it all is defining a class variable with a name other than $db.

EDIT:

To keep the current database loaded on each page load, you'll need to use sessions. In the controller function that handles the dropdown data, you could enter something similar to the following:

$db = $this->input->post('select')
$this->My_Model->getDB($db);
$this->session->set_userdata($db);

Within any controller that will use the database you'll want to add code to load the current database:

function __construct(){
    parent::__construct();
    $this->load->model('My_Model');
    $db = $this->session->userdata('db');
    $this->My_Model->getDB($db);
}

EDIT:

If you are going to need to access the same database from various models, I suggest using a library by mddd at EllisLab. Just create a PHP file named Db_manager.php with the folowing code and drop it into your application/libraries directory:

class Db_manager
{
    var $connections = array();
    var $CI;

    function __construct()
    {
        $this->CI =& get_instance();
    }

    function get_connection($db_name)
    {
        // connection exists? return it
        if (isset($this->connections[$db_name])) 
        {
            return $this->connections[$db_name];
       }
       else
       {
            // create connection. return it.
            $this->connections[$db_name] = $this->CI->load->database($db_name, true);
            return $this->connections[$db_name];
        }
    }
}

In the constructor of each model that will use the database add the following:

var $current_db;

function __construct(){
    parent::__construct();
    $this->load->library('Db_manager');
    $db = $this->session->userdata('db');
    if ($db == 1){
        $this->current_db = $this->db_manager->get_connection('default');
    } else {
        $this->current_db = $this->db_manager->get_connection('alternate');
    }
}

这篇关于如何在codeigniter中切换数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆