1

I want to auto-load my multiple database so I can use them simultaneously. I have this configuration in my /application/config/database.php:

$active_group = 'identity';
$query_builder = TRUE;

// DB Connection
$__db['hostname'] = 'localhost';
$__db['username'] = 'root';
$__db['password'] = '1234';

// Database 1
$db['identity'] = array(
    'dsn'      => '',
    'dbdriver' => 'mysqli',
    'hostname' => $__db['hostname'],
    'username' => $__db['username'],
    'password' => $__db['password'],
    'database' => 'x1zn2j_identity',
    'dbprefix' => 'id_',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'cachedir' => '',
    'swap_pre' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'encrypt'  => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

// Database 2
$db['control'] = array(
    'dsn'      => '',
    'dbdriver' => 'mysqli',
    'hostname' => $__db['hostname'],
    'username' => $__db['username'],
    'password' => $__db['password'],
    'database' => 'x1eg2x_ctrl',
    'dbprefix' => 'ctrl_',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'cachedir' => '',
    'swap_pre' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'encrypt'  => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

Now to make it more flexible because maybe in the future, I'll add a new database and just include them in my database.php config file so it will be a hassle-free framework. So what I did was I created a new library called MultiDB.php and added it to the autoload.php config file so it will be added on every page load. Then I add this code:

class MultiDB
{
    public $dbase = array();

    function __construct()
    {
        global $db;

        foreach($db as $database)
        {
            $this->dbase[$database] = $this->load->database($database, TRUE);
        }
    }
}

Now obviously, it won't work. I want it to automatically load the DB based on the $db configuration. So if I'll add a new database configuration, the new database will load and can be used using the Query Builder by CI.

Anyone?

  • How do you determine which configuration to use? Also don't use globals in codeigniter, there is no need. – Alex Feb 05 '18 at 03:44
  • I think I need to use `extends` keyword here. – Pia Wurtzbach Feb 05 '18 at 03:55
  • So how can I load the `$db` configuration then loop through it? And also how can I use `$this->load->database()` in a custom library? OR should I create my own library? – Pia Wurtzbach Feb 05 '18 at 03:57
  • Libraries don't and shouldnt use extends unless they are extending a core library like `form_validation`. Further I wouldn't do this in a library. – Alex Feb 05 '18 at 03:58
  • Hmm.. that's a big help. So, how can I achieve this thing? What should I do? Any tip? :) – Pia Wurtzbach Feb 05 '18 at 04:01

1 Answers1

1

Basically you want to create a Core MY_Controller that extends CI_Controller and loads the different configs and adds them to a public class variable. This way when you create a controller with views in application/controllers you extend MY_Controller instead of CI_Controller and thus can access all the public methods of MY_Controller including the database properties.

// application/core/MY_Controller.php
class MY_Controller extends CI_Controller {

    public $DB1, $DB2;

    public function __construct() {
        parent::__construct();
        $this->DB1 = $this->load->database('identity', TRUE);
        $this->DB2 = $this->load->database('control', TRUE);
    }

}

// application/controllers/Some_controller.php
// extend MY_Controller instead CI_Controller
class Some_controller extends MY_Controller {

    public function index() {

        $this->DB1->query(...) // query in database 1
        $this->DB2->query(...) // query in database 2
    }

}
Alex
  • 9,215
  • 8
  • 39
  • 82