Welcome Guest, Not a member yet? Register   Sign In
Connecting to multiple databases
#2

[eluser]TheFuzzy0ne[/eluser]
Assign the model a variable to store it's database object. You can use a global instead if you wish.

Code:
$this->CLIENT_DB = $this->load->database('client7', TRUE);


If you want to change databases, then you would simply reassign the new database to the global variable:

Code:
$this->CLIENT_DB = $this->load->database('client28', TRUE);

Your code can be structured to deal with the generic client database ($CLIENT_DB). It wouldn't need to know which one it's working on, you'd just have to make sure that you've loaded the right one before using it, and do any error checking at that time.

Code:
class MY_client_DB extends Model {

    var $_CLIENT_DB = FALSE; // The database object
    var $cid = FALSE; // Client's ID

    function Some_model($client_id)
    {
        parent::Model();
        $this->initialize($client_id);
    }

    function initialize($client_id)
    {
        // Load the clients database
        $this->_CLIENT_DB = $this->load->database("client" . $client_id);

        // If we get a FALSE, return FALSE
        if ($this->_CLIENT_DB == FALSE) { return FALSE; }

        // Otherwise, set the client ID for the model, and return TRUE
        $this->cid = $client_id;
        return TRUE;
    }
}

extend that class with your actual model

Code:
class Client_db extends MY_Client_db {

    // Update the clients username
    function updateClientUsername($new_username)
    {
        $this->_CLIENT_DB->where('id', $this->cid);
        $this->_CLIENT_DB->update('clients', array('username' => $new_username);
    }

    // Update the clients address
    function updateClientAddress($data)
    {
        $this->_CLIENT_DB->where('client_id', $this->cid);
        $this->_CLIENT_DB->update('client_addresses', $data);
    }

}

Load the model from within your controller and supply the client ID to the constructor (after you know it's valid, of course):

Code:
$this->load->model('client_db', $client_id);

You will get an error if you don't supply a client ID. It's up to you to ensure that the model has loaded successfully. Assuming you've loaded your model with the credentials above, you can check like this:

Code:
if ($this->client_db->cid === FALSE)
{
    // Uh-oh. No CID so either the database configuration for them doesn't exist.
    ...
}
else
{
    // Sweet. We're good to go!
    ...
}


The code above is untested. It serves only to help illustrated my thoughts.
I hope this helps.


Messages In This Thread
Connecting to multiple databases - by El Forum - 02-10-2009, 05:58 PM
Connecting to multiple databases - by El Forum - 02-11-2009, 07:13 AM
Connecting to multiple databases - by El Forum - 02-16-2009, 07:55 AM
Connecting to multiple databases - by El Forum - 02-16-2009, 08:47 AM
Connecting to multiple databases - by El Forum - 06-13-2009, 04:50 AM
Connecting to multiple databases - by El Forum - 06-13-2009, 05:18 AM
Connecting to multiple databases - by El Forum - 06-13-2009, 05:43 AM



Theme © iAndrew 2016 - Forum software by © MyBB