• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CODEIGNITER 4: Multiple Database Connection (MySQL and Oracle)

#1
I have a project that uses multiple databases, MySQL and Oracle.

In Codeigniter 3 we have database driver oci8 and mysqli that can be easily used and configured in database connection.

Below is my database connection in Codeigniter 3:

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

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

$active_group = 'oracle';
$query_builder = TRUE;

$db['oracle'] = array(
    'dsn'    => '',

    // Local setting
    // 'hostname' => 'localhost/XE',
    // 'username' => 'system',
    // 'password' => 'password',
    // 'database' => '',
    // end local setting
    
    'dbdriver' => 'oci8',
    '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
);

How can I use the same setting above in Codeigniter 4?
I noticed that in Codeigniter 4 RC 3 there is no (or not yet) database driver for Oracle.

This is my setting in Codeigniter 4:

Code:
public $defaultGroup = 'default';

    /**
     * The default database connection.
     *
     * @var array
     */
    public $default = [
        'DSN'      => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => 'PASSWORD',
        'database' => '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,
    ];

    public $defaultGroup = 'oracle';

    /**
     * The default database connection.
     *
     * @var array
     */
    public $oracle = [
        'DSN'      => '',
        // Local setting
        // 'hostname' => 'localhost/XE',
        // 'username' => 'system',
        // 'password' => 'password',
        // 'database' => 'mydtabase',
        // end local setting

        'DBDriver' => 'WHAT DRIVER SHOULD I USE?',
        '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,
    ];

Thank you for your help and kind attentions
Reply

#2
CI4, oracle not yet support.
Reply

#3
You should only need to change the group name property when loading a database.

PHP Code:
$db = \Config\Database::connect('group_name'false); 


Database settings.

PHP Code:
public $defaultGroup 'default';

    /**
     * The default database connection.
     *
     * @var array
     */
    public $default = [
        'DSN'      => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => 'PASSWORD',
        'database' => '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,
    ];

    /**
     * The default database connection.
     *
     * @var array
     */
    public $oracle = [
        'DSN'      => '',
        // Local setting
        // 'hostname' => 'localhost/XE',
        // 'username' => 'system',
        // 'password' => 'password',
        // 'database' => 'mydtabase',
        // end local setting

        'DBDriver' => 'WHAT DRIVER SHOULD I USE?',
        '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,
    ]; 

Try that when they release the oracle driver.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.