![]() |
Global Variables and Two Databases - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Global Variables and Two Databases (/showthread.php?tid=38989) Pages:
1
2
|
Global Variables and Two Databases - El Forum - 02-25-2011 [eluser]wynnewade[/eluser] I am using a "development" database and a "production" database as instructed in the manual loading the connection as $DB1 and $DB2. What I am trying to figure out (unsuccessfully)is how to put the $DB1 = $this->load->database('default', TRUE); and $DB2 = $this->load->database('pro', TRUE); into the Model Controller so that I don't have to copy/paste it into EVERY function. I tried var and const and public in the controller but still either got the variable undefined error or just a blank white page. WHAT AM I MISSING??? Thanks in advance, Jon Global Variables and Two Databases - El Forum - 02-25-2011 [eluser]LuckyFella73[/eluser] You don't have to load the database class in your model. It's loaded automatically. All you have to do to switch databases is setting up 2 configurations in config/database.php and set the active one. Code: $active_group = 'default'; Global Variables and Two Databases - El Forum - 02-25-2011 [eluser]wynnewade[/eluser] I guess I used the wrong terminology and created chaos. I AM loading my database class in the autoloader. What I meant was this: In every function I have to write something like $DB1->insert('table',$insArr); and $DB2->insert('table',$insArr); To do that, I also have to copy $DB1 = $this->load->database(‘default’, TRUE); and $DB2 = $this->load->database(‘pro’, TRUE); into each function. These variables only have function scope. However, I read where you could put those variables into the model controller and they would be available for each function without having to copy them in. That is what I am trying to figure out. Sorry for the confusion and Thank You for the quick response. Global Variables and Two Databases - El Forum - 02-25-2011 [eluser]LuckyFella73[/eluser] I'm still a bit confused.. Do you need to connect to 2 different databases simultanesly on develop server and 2 different dbs on production server at the same time? Global Variables and Two Databases - El Forum - 02-25-2011 [eluser]Jaketoolson[/eluser] You are wanting to extend the CI_Controller using MY_Controller. Assuming the variable $config['subclass_prefix] = 'MY_', you create a file in the '/applications/core' folder called 'MY_Controller.php'. MY_Controller.php: Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); Now in your other controller(s), or whichever controllers need to use both databases, you need to extend them to MY_Controller, instead of CI_Controller. Code: // from this Global Variables and Two Databases - El Forum - 02-25-2011 [eluser]wynnewade[/eluser] [quote author="LuckyFella73" date="1298672800"]I'm still a bit confused.. Do you need to connect to 2 different databases simultanesly on develop server and 2 different dbs on production server at the same time?[/quote] LOL ... it IS confusing We manage 63 student apartment properties and they each have a website. There is a single CMS that governs all of the sites. There is a development site for each property (where the property personnel can "test" their updates) and a production site which is open to the public. The CMS has to be able to read and write to both databases. Global Variables and Two Databases - El Forum - 02-25-2011 [eluser]wynnewade[/eluser] [quote author="Jaketoolson" date="1298675680"]You are wanting to extend the CI_Controller using MY_Controller. etc. [/code][/quote] quick (I hope) question ... I do the $DB1 and the $DB2 stuff in the <b>Model</b> not the Controller. Should I extend the Model or continue as you indicated? Thanks!!!!! Global Variables and Two Databases - El Forum - 02-25-2011 [eluser]Jaketoolson[/eluser] does your model have a __construct() function or anything? Global Variables and Two Databases - El Forum - 02-25-2011 [eluser]wynnewade[/eluser] yes it does. Shouldn't it? Code: class Content extends CI_Model { Global Variables and Two Databases - El Forum - 02-25-2011 [eluser]Jaketoolson[/eluser] OHHHHHHHHHHHH just do this: Code: class Content extends CI_Model { |