-
Rajakumaran Newbie

-
Posts: 1
Threads: 1
Joined: Jul 2017
Reputation:
0
hai to all,
if possible to connect multiple database in codeigniter in hooks if this possible to do?
-
PaulD Posting Freak
    
-
Posts: 1,061
Threads: 42
Joined: Mar 2015
Reputation:
73
-
HEKuiper Junior Member
 
-
Posts: 17
Threads: 5
Joined: Aug 2017
Reputation:
0
(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
-
Kaosweaver Member
  
-
Posts: 133
Threads: 19
Joined: Jan 2015
Reputation:
14
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())); }
-
dave friend Posting Freak
    
-
Posts: 1,015
Threads: 15
Joined: Jun 2015
Reputation:
50
(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.
-
HEKuiper Junior Member
 
-
Posts: 17
Threads: 5
Joined: Aug 2017
Reputation:
0
(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
|