Codeigniter 3 MY_Model database

0

I'm developing an application where each client will have their database. My scenario then will be: Client will login and through his ID apply the configuration of the bank for that client. Then, in addition to the default database, it will have a second where it will be related to that client, through its ID. In order for me to use the session ID I thought I would create a My_model and put the second database there and make an extends on the models I'm going to use that second database. But I can not do it, follow the code as it is. Thank you for helping me.

<?php

class MY_Model extends CI_Model
{

protected $active_group;
protected $db2;  

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;

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

      //$db2 = $this->load->database('second', TRUE);
    $this->db2 = $db;
}

}
    
asked by anonymous 12.12.2018 / 20:33

1 answer

0

By checking the documentation of Codeigniter 3, the solution they indicated to create dynamic connections without pre-set parameters in the database.php file, you would use the $this->load->database() function passing as a parameter a Data Source Name (DNS) variable. Something similar to this:

$dsn = 'dbdriver://username:password@hostname/database';
$this->load->database($dsn);

Using the parameters you entered would look something like this:

<?php

class MY_Model extends CI_Model
{

    protected $active_group;
    protected $db2;  

    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;

        $dsn = 'mysqli://root:@localhost/nomedobanco';
        $this->db2 = $this->load->database($dsn);
    }

}
    
12.12.2018 / 22:38