CodeIgnite changes the default database name on the fly

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

The reason for this is because I want to set up a cron script that runs commands in specific databases. I need to be able to dynamically change this and edit the /config/database.php application 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 */
0
source share
4 answers

As you want, I think this is not possible. You can do it in an alternative way.

1. .
2. , $this->load->database('site') $this->load->database('default') .

+1

3 ( )

  • , fileName.txt( ) database.php

  • ( , php)?. ,

  • ( ), $var .

0

The db specification for the entire Code Igniter application depends on the configuration specification, and this cannot be changed dynamically. This happens inside the system, and it does not allow you to change it unless you change it using a text editor.

I would move the db name in the application code dynamically by setting:

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

in the second dynamic catch:

$this->load->database('db2')
0
source
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();
    }
}
0
source

Source: https://habr.com/ru/post/1610368/


All Articles