CodeIgnite change default database name on the fly

2019-07-28 23:13发布

问题:

I have codeigniter setup to connect to 2 databases (default and site). I need to change the default database name dynamically and anything that uses the default connection should also use the new database. Is there a way to do this?

The reason for this is I want to setup a cron script that runs commands on certain databases. I need to be able to dynamically change this and editing application/config/database.php is not possible.

$active_group = 'default';
$active_record = TRUE;
$phppos_client_name = substr($_SERVER['HTTP_HOST'], 0, strpos($_SERVER['HTTP_HOST'], '.'));

$db['site']['hostname'] = 'php-pos-db';
$db['site']['username'] = 'phppoint';
$db['site']['password'] = 'password';
$db['site']['database'] = 'phppoint_site';
$db['site']['dbdriver'] = 'mysql';
$db['site']['dbprefix'] = '';
$db['site']['pconnect'] = FALSE;
$db['site']['db_debug'] = FALSE;
$db['site']['cache_on'] = FALSE;
$db['site']['cachedir'] = '';
$db['site']['char_set'] = 'utf8';
$db['site']['dbcollat'] = 'utf8_general_ci';
$db['site']['swap_pre'] = '';
$db['site']['autoinit'] = TRUE;
$db['site']['stricton'] = FALSE;

$db['default']['hostname'] = "php-pos-db";
$db['default']['username'] = "phppoint";
$db['default']['password'] = "password";
$db['default']['database'] = "phppoint_$phppos_client_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "phppos_";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_unicode_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;


/* End of file database.php */
/* Location: ./application/config/database.php */

回答1:

The way you want I think its not possible. you can do it alternative way.

1.Do not load database at your autoload config.
2.load your database manually using $this->load->database('site') or $this->load->database('default') before executing query.



回答2:

3 approaches (there could be many though)

  1. Read the whole file, save it as fileName.txt (having the updated database name) and rename the file as database.php

  2. Where will you store the database name (if it is in a file, read the value from file and place it here in the php)?. You will have to make it messy by adding the code here in this file

  3. if you saving the database name into another database (that code would be static), fetch the name and save into a $var variable and assign it in the config.



回答3:

The db specification for the entire Code Igniter app depends on the config specification and this can't be changed dynamically. This lives inside the system, it's not allowed to be modified unless you modify it with a text editor.

I would over-ride the db name in the application code dynamically by putting:

$this->load->database('db1')

on 2nd dynamic catch include:

$this->load->database('db2')


回答4:

class CronModel extends CI_Model 
{
    public function run()
    {
        // The default database configuration $db['default']
        $query1 = $this->db->query();

        // pass true as second param to use active record
        // ie : $site->select()->get();
        $site = $this->load->database('site', true);

        // The site database configuration $db['site']
        $query2 = $site->query();
    }
}