Multiple Database Connection - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11) +--- Thread: Multiple Database Connection (/showthread.php?tid=68497) |
Multiple Database Connection - Rajakumaran - 07-19-2017 hai to all, if possible to connect multiple database in codeigniter in hooks if this possible to do? RE: Multiple Database Connection - PaulD - 07-19-2017 I have never had the need to use it but yes, you can: Docs https://www.codeigniter.com/user_guide/database/connecting.html#connecting-to-multiple-databases I am pretty sure that this should apply to hooks to, but not if the hook point is before the db connection is made, for instance, 'pre-system' hook. https://www.codeigniter.com/user_guide/general/hooks.html#hooks-extending-the-framework-core Not 100% on this, but thought that might help. Paul. RE: Multiple Database Connection - ciadvantage - 07-20-2017 I m using three DBs on my company projects. it is easy as 1,2,3. Configure it from database.php RE: Multiple Database Connection - HEKuiper - 10-04-2017 (07-20-2017, 11:20 AM)ciadvantage Wrote: I m using three DBs on my company projects. it is easy as 1,2,3. Configure it from database.php In the User Guide it says: Connecting to Multiple Databases If you need to connect to more than one database simultaneously you can do so as follows: $DB1 = $this->load->database('group_one', TRUE); $DB2 = $this->load->database('group_two', TRUE); Is this to be placed in each model? Please enlighten me with a more detailed explanation!! Thanks in advance, HEKuiper RE: Multiple Database Connection - Kaosweaver - 10-04-2017 We do this by creating a connections file in the models folder (connections.php): PHP Code: class Connections extends CI_Model { Then in our models, we have: PHP Code: class Orders extends Connections { RE: Multiple Database Connection - dave friend - 10-04-2017 (10-04-2017, 03:26 AM)HEKuiper Wrote: In the User Guide it says: It can be done in a number of different ways. The main thing to understand is that the var set by the return from $this->load->database('some_group', TRUE) has to be visible (in scope) in the model that uses it. In other words, using this statement PHP Code: $DB2 = $this->load->database('db_config2', TRUE); The var $DB2 needs to be in scope to the model that uses it. You could make $DB2 a public property of a Controller. PHP Code: Class Welcome extends CI_Controller And then use DB2 in "othermodel" PHP Code: class Othermodel extends CI_Model $this->DB2 could be used in any number of other models the controller requires. I don't intend to promote this approach as optimal or even truly desirable. Arguments against this approach are easily raised. Rather, the intent is only to demonstrate another way to make a connection visible to multiple models. Ultimately "where" you load additional databases probably depends more on how often you need more than one. That said, @Kaosweaver's answer is a great example of how easily create multiple connections in an arbitrary number of model classes. RE: Multiple Database Connection - HEKuiper - 10-04-2017 (10-04-2017, 08:13 AM)dave friend Wrote:(10-04-2017, 03:26 AM)HEKuiper Wrote: In the User Guide it says: Wow this is way less code!! Thanks. I have 3 websites and 4 databases - the 4th one I want to share commonalities with all 3 websites. Example: create 1 blog post and display it on 3 websites... Taking DRY to a whole new level!! :-) Again, Thanks!! HEKuiper RE: Multiple Database Connection - InsiteFX - 10-05-2017 You can create a MY_Model and place all the code you use all the time in it, then extend all your Models from the MY_Model. |