Welcome Guest, Not a member yet? Register   Sign In
Global Variables and Two Databases
#1

[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
#2

[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';
#$active_group = 'pro'; // uncomment this line to switch to productive DB
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'Your_Username';
$db['default']['password'] = 'Your_Password';
$db['default']['database'] = 'Your_Database';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

$db['pro']['hostname'] = 'localhost';
$db['pro']['username'] = 'Your_Username';
$db['pro']['password'] = 'Your_Password';
$db['pro']['database'] = 'Your_Database';
$db['pro']['dbdriver'] = 'mysql';
$db['pro']['dbprefix'] = '';
$db['pro']['pconnect'] = TRUE;
$db['pro']['db_debug'] = TRUE;
$db['pro']['cache_on'] = FALSE;
$db['pro']['cachedir'] = '';
$db['pro']['char_set'] = 'utf8';
$db['pro']['dbcollat'] = 'utf8_general_ci';
$db['pro']['swap_pre'] = '';
$db['pro']['autoinit'] = TRUE;
$db['pro']['stricton'] = FALSE;
#3

[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.
#4

[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?
#5

[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');
        
class MY_Controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();

        $DB1 = $this->load->database('default', TRUE);
        $DB2 = $this->load->database('pro', TRUE);
    }
}


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
class controller_name extends CI_Controller {}

// to this
class controller_name extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $DB1 // is accessible
        $DB2 // is accessible
    }
}
#6

[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.
#7

[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!!!!!
#8

[eluser]Jaketoolson[/eluser]
does your model have a __construct() function or anything?
#9

[eluser]wynnewade[/eluser]
yes it does. Shouldn't it?

Code:
class Content extends CI_Model {
function __construct() {
        parent::__construct();
} // end constructor
/////////////////////////////////////////////
#10

[eluser]Jaketoolson[/eluser]
OHHHHHHHHHHHH

just do this:

Code:
class Content extends CI_Model {
function __construct() {
        parent::__construct();
        $this->DB1 = $this->load->database('default', TRUE);
        $this->DB2 = $this->load->database('pro', TRUE);
}

function test(){
        $this->DB1 // is accessible
        $this->DB2 // is accessible
}




Theme © iAndrew 2016 - Forum software by © MyBB