1

For every project that I made, I always load database configuration in my construct function as below

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

For some reason, I want to load different configuration for every function inside one model as below

class Something_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    public function getAll() {
        $this->load->database('DB1', TRUE);
        // some code to be executed
    }

    public function getPart() {
        $this->load->database('DB2', TRUE);
        // some code to be executed
    }
}

I need some advice about those new way loading database configuration. Is there any performance problem when I load the database in every function? Or is it safe to use?

Plus, is there any suggestion to call different database for different function inside one model?

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
imilah
  • 131
  • 2
  • 15

3 Answers3

2

You will always experience some performance loss by doing this, its just par for the course. How much can be determined through testing, but perhaps the CI profiler could help you figure that out.

But if you only use a few different databases, and use them often you may benefit from globally initializing them in a MY_Controller so you don't have to initialize the connection every-time you call a function.

class MY_Controller extends CI_Controller {
    public $DB1, $DB2;

    public function __construct() {
        parent::__construct();
        $this->DB1 = $this->load->database('DB1', TRUE);
        $this->DB2 = $this->load->database('DB2', TRUE);
    }

}

class Some_controller extends MY_Controller {

    public function __construct() {
        $this->load->model('somemodel');
    }

}

//https://www.codeigniter.com/userguide3/database/connecting.html#connecting-to-multiple-databases
class Some_model extends CI_Model {

    public function access_db_1() {
        $this->DB1->query('...');
    }

    public function access_db_2() {
        $this->DB2->query('...');
    }

}

Of course if you didn't need multiple configs you could:

You don’t need to create separate database configurations if you only need to use a different database on the same connection. You can switch to a different database when you need to, like this:

$this->db->db_select($database2_name);

Alex
  • 9,215
  • 8
  • 39
  • 82
  • I know it's been 5 years since you answer this topic, but I choose your suggestion to repair my code. Thank you – imilah Mar 02 '23 at 05:49
0

I think it's save to use and it should be no problem using different database configuration on different function, but I also expect some performance difference since each of your function make a different database connection

Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
0

Is there any performance problem when I load database in every function?

No. Not at all. each and every function treat individually. So there is no any performance issue with DB connection. Unless

  1. DB connection with remote server
  2. DB is not fully compatible with PHP. (Means MySQL treats more faster than Oracle)
  3. Worst case memory leak in code

is it safe to use?

Whatever the method you're using associated with the CodeIgniter is secured. Keep using their inbuilt function/methods and care about their security methods


Plus, is there any suggestion to call different database for different function inside one model?

Yes, there're many. But there is no standard (do like this) way. Which you having now is also look perfect to me. Even you can create those methods in config.php and include in your models. But there no different with all of these.


Suggestion

If you're having multiple DB connections then, keep DB1 as default connection and in __construct you can load that by using $this->load->database();. And whenever you need to load second database connection, then use $this->load->database('DB2', TRUE);.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85