• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple Database Connection

#1
hai  to all,
   if possible to connect multiple database in codeigniter in hooks if this possible to do?
Reply

#2
I have never had the need to use it but yes, you can:
Docs https://www.codeigniter.com/user_guide/d...-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/g...ework-core

Not 100% on this, but thought that might help.

Paul.
Reply

#3
I m using three DBs on my company projects. it is easy as 1,2,3. Configure it from database.php
Reply

#4
(07-20-2017, 12:20 PM)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
Reply

#5
We do this by creating a connections file in the models folder (connections.php):
PHP Code:
class Connections extends CI_Model {

 var 
$db='';

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

 
$config['dbdriver'] = "mysqli";
 
$config['dbprefix'] = "";
 
$config['pconnect'] = FALSE;
 
$config['db_debug'] = FALSE;
 
$config['cache_on'] = FALSE;
 
$config['cachedir'] = "";
 
$config['char_set'] = "utf8";
 
$config['dbcollat'] = "utf8_general_ci";
 
$config['hostname'] = XXXXXXXXXXXXXXXXXXXX;
 
$config['username'] = XXXXXXXXXXXXXXXXXXXX;
 
$config['password'] = XXXXXXXXXXXXXXXXXXXX;
 
$config['database'] = XXXXXXXXXXXXXXXXXXXX;

 
$this->DB2=$this->load->database($config,TRUE);
 }

 function 
_db_two() {

 
$config['dbdriver'] = "mysqli";
 
$config['dbprefix'] = "";
 
$config['pconnect'] = FALSE;
 
$config['db_debug'] = FALSE;
 
$config['cache_on'] = FALSE;
 
$config['cachedir'] = "";
 
$config['char_set'] = "utf8";
 
$config['dbcollat'] = "utf8_general_ci";
 
$config['hostname'] = XXXXXXXXXXXXXXXXXXXX;
 
$config['username'] = XXXXXXXXXXXXXXXXXXXX;
 
$config['password'] = XXXXXXXXXXXXXXXXXXXX;
 
$config['database'] = XXXXXXXXXXXXXXXXXXXX;

 
$this->DB2=$this->load->database($config,TRUE);
 }



Then in our models, we have:

PHP Code:
class Orders extends Connections {

 function 
__construct() {
 
  // Call the Model constructor
 
  parent::__construct();
 
  $this->_db_one();
 }
public function 
adjust_cart($cart_key$productid$qty) {
 
   $this->DB2->where('sessionid'$cart_key); 
 
   $this->DB2->where('productid'$productid); 
 
   $this->DB2->update($this->tbl_order_storage, array('qty'=>$qty'date'=>time()));   
  

Reply

#6
(10-04-2017, 04: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.
Reply

#7
(10-04-2017, 09:13 AM)dave friend Wrote:
(10-04-2017, 04: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

#8
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.
What did you Try? What did you Get? What did you Expect?

Joined the CodeIgniter Community in 2009.          ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.