0
I’m developing an application where every customer will have their database. My scenario then will be: Client will log in and through his ID apply the bank configuration for this client. So in addition to the default database, you will get a second where it will be related to that client, through your ID. So I can use the session ID I thought of creating a My_model
and put the second database there and do a extends on the models I’m going to use that second bank. But I’m not able to do it, follow the code of how it is. I thank anyone who can help 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;
}
}
I made these changes and presents error as if I did not recognize the database see in my reply, I had to do separated by character limit
– Thiago Moreira
@Thiagomoreira What exactly is the error message presented by PHP?
– Pedro Souza
Sorry I made the wrong answer, is that I’m starting to use stack now. The error shown is this: Message: Call to a Member Function select() on Boolean I will pass you the Pastebin link with part of the https://pastebin.com/dFGXETTfcode
– Thiago Moreira
The problem then is in the data you are informing for connection. $this->load->database probably returns false (which is the one that he’s talking about). Check if the host, username, password and bank name are correct.
– Pedro Souza
the seat settings are correct. Then if I put $this->db2 = $this->load->database( 'Second', TRUE ); it displays this message: You have specified an invalid database Connection group (Second) in your config/database.php file. But in my database.php file is the main connection only that I want and this message as if it were assigning this second database in the database file
– Thiago Moreira
@Thiagomoreira I rephrased the answer. Take a look if it suits you.
– Pedro Souza
yes, answer that way!
– Thiago Moreira
@Great Thiagomoreira then. If I was solved, you can accept my answer.
– Pedro Souza
I still can’t solve
– Thiago Moreira