Welcome Guest, Not a member yet? Register   Sign In
Switch Database
#1

Hello everyone

I have searched the forum for a solution to my problem but have not found an answer

I have to change the name of the database depending on who connects to the web page

the only data that changes is the name of the db (DB1, DB2, DB3, DB4 .......)


Code:
$active_group = 'default';
$query_builder = TRUE;

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

I have to change only this data 'database' => 'DB1', in the array, passing a variable

You can help me?

thank you so much
Reply
#2

(This post was last modified: 04-13-2020, 06:37 PM by cyberstunts.)

Documentation on Working With Databases provides with 2 straight forward ways:

  1. Define groups in configuration
  2. Load custom connection during page execution


In more detail:

Option 1:

Add connections by copy, pasting and renaming 'public $default' to e.g. 'public $my_db_1', 'public $my_db_2', and when want to choose which one to use, just load the DB config you added with:

  
PHP Code:
$db_1 = \Config\Database::connect('my_db_1');
$db_2 = \Config\Database::connect('my_db_2');

// Or choose group dynamically based on your defined database connections e.g.
$db_name 'my_db_' $db_number;    // app/Config/Database.php definitions of groups still apply
$db = \Config\Database::connect($db_name); 

Option 2:

Similar initiation, just instead passing an array which you can change while running your model:

PHP Code:
$custom = [
            'DSN'      => '',
            'hostname' => 'localhost',
            'username' => '',
            'password' => '',
            'database' => '',
            'DBDriver' => 'MySQLi',
            'DBPrefix' => '',
            'pConnect' => false,
            'DBDebug'  => (ENVIRONMENT !== 'production'),
            'cacheOn'  => false,
            'cacheDir' => '',
            'charset'  => 'utf8',
            'DBCollat' => 'utf8_general_ci',
            'swapPre'  => '',
            'encrypt'  => false,
            'compress' => false,
            'strictOn' => false,
            'failover' => [],
            'port'     => 3306,
    ];

// Wrap in IF with your logic for database connections
    $custom['database'] = 'DB3';
// End if

$db = \Config\Database::connect($custom); 

EDIT:

Just realised that you posted under CI3.

Your requirement is supported out of the box even in CI3, described here in "Connecting to your Database". There's all the flexibility you need. The logic is the same. 

I will leave CI4 version as is.
Reply
#3

Option 2:

Similar initiation, just instead passing an array which you can change while running your model:

PHP Code:
$custom = [
            'DSN'      => '',
            'hostname' => 'localhost',
            'username' => '',
            'password' => '',
            'database' => '',
            'DBDriver' => 'MySQLi',
            'DBPrefix' => '',
            'pConnect' => false,
            'DBDebug'  => (ENVIRONMENT !== 'production'),
            'cacheOn'  => false,
            'cacheDir' => '',
            'charset'  => 'utf8',
            'DBCollat' => 'utf8_general_ci',
            'swapPre'  => '',
            'encrypt'  => false,
            'compress' => false,
            'strictOn' => false,
            'failover' => [],
            'port'     => 3306,
    ];

// Wrap in IF with your logic for database connections
    $custom['database'] = 'DB3';
// End if

$db = \Config\Database::connect($custom); 

sorry but i don't understand very well Huh

Do I have to enter this code in each model?

Can't I use config/database.php and just pass the database parameter?


Thanks a lot for your help Smile
Reply
#4

I think one of them could suite you when loading a model (from docs):
Use this in Controller.
PHP Code:
$db_config_file $this->config->load('database');
$config $this->config->item('default''database');

// Load model with DB1
$this->load->model('model_name'''$config);

// Load model with DB2
$config['database'] = 'DB2';
$this->load->model('model2_name'''$config);

// Load model with DB3
$config['database'] = 'DB3';
$this->load->model('model3_name'''$config); 

There're more advanced ways. In CI3, you have plenty of other options. But you will need to try coding it in first to see how things work.

You can also load database configuration using config library, then load them into models.

I can't guess what will suite you, but you need to see what's on the table and decide what you want to use and make your own approach. 

Various ways of loading configuration and database connections was especially made for you with dozens of ways to integrate your logic.

Controllers and Models are extensible, and could allow you to prepare connection before you load controllers or classes.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB