Codeigniter 3 My_model database

Asked

Viewed 64 times

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;
}

}

1 answer

0

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

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

Using the parameters you reported, it would be 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);
    }

}
  • 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

  • @Thiagomoreira What exactly is the error message presented by PHP?

  • 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

  • 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.

  • 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

  • @Thiagomoreira I rephrased the answer. Take a look if it suits you.

  • yes, answer that way!

  • @Great Thiagomoreira then. If I was solved, you can accept my answer.

  • I still can’t solve

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.