Multiple codeigniter database connections

2

I've been investigating the use of multiple databases with CodeIgniter. If I know what the databases are ahead of time, I can set the information in the configuration file and then call the database group I need.

In my situation, however, I need to store this database information in another database. It is a kind of master database with general information about a client, including the database and the credentials on which the client data is stored. This provider can then add clients whenever they want and have segregated the data of each client in different databases.

How can I configure the database and credentials based on the values I get from the master database in CodeIgniter, or is there even a way to do this?

I need this because I have a unique system and each client that logs in has a single database for it, so imagine that I can have 5 people using the system at the same time but each of them accessing their own database.

If anyone can help me thank you!

Abs

    
asked by anonymous 25.07.2017 / 17:13

1 answer

2

In general you have only 1 database configured in the /application/config/database.php file of CodeIgniter and that by default the configuration is called default :

// Isto já existe
$db['default'] = array(
    'dsn'      => '',
    'hostname' => '80.45.168.49',
    'username' => 'steven.douglas',
    'password' => 'hEo&73T#@wToh',
    'database' => 'main_db',
    '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
);

You'll need to set up another one shortly after, and name it, for example, auxiliar :

// Isto terá que ser adicionado
$db['auxiliar'] = array(
    'dsn'      => '',
    'hostname' => '80.45.168.50',
    'username' => 'john.eastwood',
    'password' => 'cEsl#$tAnw4Mh',
    'database' => 'another_bd',
    '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
);

And looking at the class MY_Model, which is named just because it is for you to customize it, you see that the default setting is associated with the $db variable. Then you will have to create a variable like $db2 , or a name of your own, for your auxiliar configuration, editing the file /application/core/MY_Model.php :

class MY_Model extends CI_Model
{
    protected $active_group;
    protected $db2; // <------ ADICIONE 

    public function __construct() {
        parent::__construct();
        $this->connect();
    }

    public function __destruct() {
        $this->db->close();
    }

    public function connect($active_group = 'default'){
        $this->active_group = $active_group;
        $db = $this->load->database($active_group, TRUE);
        $this->db = $db;

        // ADICIONE ISTO ABAIXO
        $db2 = $this->load->database('auxiliar', TRUE);
        $this->db2 = $db2;
    }

}
Ready! now you can use $this->db2 in your models :

$query = $this->db2->select('nome_pessoa, numero_cpf')->get('pessoas');
    
26.07.2017 / 14:01