Welcome Guest, Not a member yet? Register   Sign In
Multiple Database Connection
#7

(10-04-2017, 08:13 AM)dave friend Wrote:
(10-04-2017, 03:26 AM)HEKuiper Wrote: 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

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
{
 
   public $DB2;

 
   function __construct()
 
   {
 
       parent::__construct();
 
       $this->DB2 $this->load->database('db_config2'true);
 
       $this->load->model('othermodel');
 
   }

 
   public function index()
 
   {
 
       echo $this->othermodel->get_event();
 
   

And then use DB2 in "othermodel"

PHP Code:
class Othermodel extends CI_Model
{
   public function get_event()
   {

       return $this->DB2
           
->query("Select eventtitle from events where eventid = 20")
           ->row()
           ->eventtitle;
   }


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

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
Reply


Messages In This Thread
Multiple Database Connection - by Rajakumaran - 07-19-2017, 03:22 AM
RE: Multiple Database Connection - by PaulD - 07-19-2017, 10:55 AM
RE: Multiple Database Connection - by ciadvantage - 07-20-2017, 11:20 AM
RE: Multiple Database Connection - by HEKuiper - 10-04-2017, 03:26 AM
RE: Multiple Database Connection - by dave friend - 10-04-2017, 08:13 AM
RE: Multiple Database Connection - by HEKuiper - 10-04-2017, 01:34 PM
RE: Multiple Database Connection - by Kaosweaver - 10-04-2017, 06:33 AM
RE: Multiple Database Connection - by InsiteFX - 10-05-2017, 03:02 AM



Theme © iAndrew 2016 - Forum software by © MyBB