Where to define database when not autoloading? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Where to define database when not autoloading? (/showthread.php?tid=65088) |
Where to define database when not autoloading? - daveaton - 04-27-2016 Hi. I have 2 databases and I want to optimize my site. Should I call PHP Code: $this->db1 = $this->load->database('somegroupname1', true); From the Controller or from the Model ? I am worried that my controller loading 3 to 5 models should I call the database from the controller or from the model. Some models use 1 database and some use another. Some models do not require any models and some only require 1 database connection. I am trying to reduce database connections.. Thanks RE: Where to define database when not autoloading? - mwhitney - 04-29-2016 You could extend the loader and add 2 methods to connect to your databases (along with two properties in the super object to reference them). The easiest way to do so would be to create a "MY_Loader" class and copy the database() method from the CI loader into your loader, then refactor each copy to use unique method names and reference unique properties on the $CI instance. In other words, something like this: PHP Code: <?php Then you would use it like this: PHP Code: $this->load->databaseOne(); The loader would then manage the connection(s), returning the open connection if it already exists. With some extra work, it's probably possible to do this with less code, but this is the easiest/simplest solution that came to mind. |