如何在代码点火器中动态加载数据库设置

时间:2018-10-08 07:29:36

标签: database codeigniter codeigniter-3 environment saas

我需要处理已经在codeigniter上构建的代码。项目更多是REST API后端。他们有两个要求,首先需要从特定的HTTP标头clientid动态加载数据库。所以我可以拥有DB_1234,DB_1235之类的数据库。我需要基于此加载数据库。

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

  1. 从application / database.php删除数据库配置
  2. 使用http标头clientid的参数制作了一个辅助函数,以获取特定客户端的数据库配置数组。
  3. 在具有$this->load->database($config, true);调用的模型中,
  4. 调用帮助函数。

我想用一种比使用辅助函数更好的方法替换它,并将负载数据库代码推送到每个模型文件中。

我还需要添加基于环境的设置,因此它们也是代码点火器建议的两种方式。在环境中使用config中的不同文件夹,然后将配置文件推送到其中,然后使用多维数组在第二个数据库特定的文件夹中使用多维数组定义环境。

那么可以推荐什么方法。我对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中的多个数据库设置将不起作用。

1 个答案:

答案 0 :(得分:1)

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

Caused by: java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. null
    at org.apache.catalina.connector.Request.parseParts(Request.java:2932) ~[catalina.jar:8.5.31]
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3232) ~[catalina.jar:8.5.31]
    at org.apache.catalina.connector.Request.getParameter(Request.java:1137) ~[catalina.jar:8.5.31]
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381) ~[catalina.jar:8.5.31]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... 35 more
Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. null
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:297) ~[tomcat-coyote.jar:8.5.31]
    at org.apache.catalina.connector.Request.parseParts(Request.java:2884) ~[catalina.jar:8.5.31]
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3232) ~[catalina.jar:8.5.31]
    at org.apache.catalina.connector.Request.getParameter(Request.java:1137) ~[catalina.jar:8.5.31]
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381) ~[catalina.jar:8.5.31]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... 35 more
Caused by: java.net.SocketTimeoutException
    at org.apache.tomcat.util.net.NioBlockingSelector.read(NioBlockingSelector.java:201) ~[tomcat-coyote.jar:8.5.31]
    at org.apache.tomcat.util.net.NioSelectorPool.read(NioSelectorPool.java:235) ~[tomcat-coyote.jar:8.5.31]
    at org.apache.tomcat.util.net.NioSelectorPool.read(NioSelectorPool.java:216) ~[tomcat-coyote.jar:8.5.31]
    at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1250) ~[tomcat-coyote.jar:8.5.31]
    at org.apache.tomcat.util.net.NioEnd

point$NioSocketWrapper.read(NioEndpoint.java:1193) ~[tomcat-coyote.jar:8.5.31]

);

$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

);

使用第二个数据库时,语法如下。

$db['seconddatabase'] = array(
'dsn'   => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'seconddatabase',
'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