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');