Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 如何在CodeIgniter中动态加载数据库设置_Database_Codeigniter_Codeigniter 3_Environment_Saas - Fatal编程技术网

Database 如何在CodeIgniter中动态加载数据库设置

Database 如何在CodeIgniter中动态加载数据库设置,database,codeigniter,codeigniter-3,environment,saas,Database,Codeigniter,Codeigniter 3,Environment,Saas,我需要在codeigniter上处理已经生成的代码。项目更像是一个RESTAPI后端。它们有两个要求,第一个数据库需要从特定的http头clientid动态加载。所以我可以拥有像DB_1234,DB_1235这样的DB。我需要在此基础上加载DB 以前的开发人员使用以下方法 从application/database.php中删除数据库配置 创建了一个带有http头clientid参数的帮助函数,以获取特定客户端的db配置数组 在带有$this->load->database($config,tr

我需要在codeigniter上处理已经生成的代码。项目更像是一个RESTAPI后端。它们有两个要求,第一个数据库需要从特定的http头clientid动态加载。所以我可以拥有像DB_1234,DB_1235这样的DB。我需要在此基础上加载DB

以前的开发人员使用以下方法

  • 从application/database.php中删除数据库配置
  • 创建了一个带有http头clientid参数的帮助函数,以获取特定客户端的db配置数组
  • 在带有
    $this->load->database($config,true)的模型中调用helper函数呼叫
    我想将其替换为比使用helper函数更好的方法,并将加载数据库代码推送到每个模型文件中

    此外,我还需要添加基于环境的设置,以便它们也是双向代码点火器。在config for environment中使用不同的文件夹,并将配置文件推送到其中,然后使用多维数组定义特定于DB的环境

    那么,我们可以推荐什么样的方法呢。我没有太多接触codeigniter

    助手代码

    function getDbConfig($customer_db) {
        if ($customer_db)
             $customerDatabase = strtolower($customer_db);
        else
             $customerDatabase = 'defaultdb';
    
        $config['hostname'] = 'localhost';
        $config['username'] = 'root';
        $config['password'] = 'root';
        $config['database'] = 'DB_'.$customerDatabase;
        $config['dbdriver'] = 'mysqli';
        $config['dbprefix'] = '';
        //$config['port']     = 3306;
        $config['pconnect'] = FALSE;
        $config['db_debug'] = FALSE;
        $config['cache_on'] = FALSE;
        $config['cachedir'] = '';
        $config['char_set'] = 'utf8';
        $config['dbcollat'] = 'utf8_general_ci';
        return $config;
    }
    
    模型文件代码

    public function __construct() {
        ob_start();
        parent::__construct();
        $this->header = apache_request_headers();
        $config = getDbConfig($this->header['clientid']);
        $this->customDb = $this->load->database($config, true);
    }
    
    注意:我知道在直接尝试建立连接之前,代码应该清理并检查db是否存在。这需要以后解决


    注:对于特殊要求,项目将一次提供一个DB。但该请求将具有http头clientid,用于确定需要连接的数据库。因为将有n个客户机和n个数据库,所以database.php中的多个数据库设置将不起作用。

    您可以使用database.php中的以下代码,使用codigniter数据库文件使用两个数据库

    $db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'firstdatabase',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => TRUE,
    'cachedir' => 'application/cache',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
    
    ))

    ))

    对于使用第二个数据库,语法如下

    $otherdb = $this->load->database('seconddatabase', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.
    
    $query = $otherdb->select('column_one, column_two')->get('table');
    

    我需要动态连接到DBs。它们将是未知数量的分贝。对于有限且已知数量的数据库,建议使用您的方法,因为我们需要一次从多个数据库获取数据。在我的例子中,一个请求只消耗一个DB,但该DB将取决于HTTP头变量。
    $otherdb = $this->load->database('seconddatabase', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.
    
    $query = $otherdb->select('column_one, column_two')->get('table');