动态数据库切换-Codeigniter [英] Dynamic Database Switch - Codeigniter

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

问题描述

我想在运行时切换我的codeigniter多个数据库。我的默认数据库将在该页面上完全运行,但是当我需要根据场景或要求切换到其他数据库时,我可以这样做。 通用模型功能将适用于所有不同的数据库。 因此,我想通过动态选择器在不使用会话或不传递函数变量的情况下对多个数据库连接使用相同的模型和相同的功能

I want to switch my codeigniter multiple database on runtime. My Default database will run thoroughly the page but when I needed to switch to other database based on scenario or requirement then I can do. The common model function will work for the all different databases. So, I want to use same model and same function for multiple database connection using dynamic selector without using session or without passing function variable

要实现此目的,我在cofig中设置了一个名称,当我调用模型时,我在调用模型之前在控制器中设置了所需的数据库名称,然后尝试在控制器中设置的模型中获取名称。但是不幸的是我没有从控制器到模型的名称。

To achieve this I set a name in cofig and when I call my model I set the required database name in controller before calling model and then I tried to get the name in model which is set in controller. But unfortunately I'm not getting the name from controller to model.

数据库配置文件-

$db['default'] = array(
               'dsn'    => '',
               'hostname' => 'localhost',
               'username' => 'root',
               'password' => 'pass'
               'database' => 'db1'
                .......
               );


$db['anotherDB'] = array(
               'dsn'    => '',
               'hostname' => 'localhost',
               'username' => 'root',
               'password' => 'pass'
               'database' => 'db2'
                .......
               );

控制器-

$this->config->set_item('active_db', 'anotherDB');
$sql = 'select * from user';
$anotherDB_record = $this->model->customQuery($sql);

print_r($anotherDB_record);


$this->config->set_item('active_db', 'default');
$sql = 'select * from customer';
$default_record = $this->model->customQuery($sql);

print_r($default_record);

模式-

   protected $database;
   function __construct() {
       parent::__construct();
       $oDB = $this->config->item('active_db');
       $this->database = $this->load->database($oDB, TRUE);
   }    
   function customQuery($sql){
       $query = $this->database->query( $sql );
       return $query->result();
   }

这是我尝试切换数据库的方式。如果你们还有其他最佳的解决方案来切换多个数据库,那么请随时向我建议。

This is the way I have tried to switch my database. If you guys have any other best solution to switch multiple database then please feel free to suggest me.

推荐答案

尝试以下动态示例配置另一个数据库

Try bellow example for dynamically configure another database

通用模型

function getOtherDB($groupID) {
    $getRecord = $this->common_model->getRow('group_master', 'GroupID', $groupID);
    if ($getRecord) {
        $config['database'] = $getRecord->DBName;
        $config['hostname'] = $getRecord->DBHostIP;
        $config['username'] = $getRecord->DBHostUName;
        $config['password'] = $getRecord->DBHostPassw;
        $config['dbdriver'] = "mysqli";
        $config['dbprefix'] = "";
        $config['pconnect'] = FALSE;
        $config['db_debug'] = TRUE;
        $DB2 = $this->load->database($config, TRUE);
        if ($DB2) {
            return $DB2;
        }
    }
    return FALSE;
}

在上面的示例中,我有group_master表,其中包含按组的数据库详细信息传递GroupId我获取记录并根据组确定另一个数据库确保您存储在数据库中的所有数据库配置信息都是加密格式,请使用以下示例在其他数据库上触发查询

In the above example, I have group_master table which has group wise database details by passing GroupId I fetch the record and set Another database according to group Make sure your all database configuration information stored in the database is encrypted format Use below example to fire query on Other database

$result = $DB2->query("select * from group");
// $DB2 is other database instance you can create multiple db connection using above methos

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

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