Welcome Guest, Not a member yet? Register   Sign In
Switch Database
#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


Messages In This Thread
Switch Database - by federcla - 04-13-2020, 10:44 AM
RE: Switch Database - by cyberstunts - 04-13-2020, 05:04 PM
RE: Switch Database - by federcla - 04-14-2020, 03:43 AM
RE: Switch Database - by cyberstunts - 04-14-2020, 05:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB