Welcome Guest, Not a member yet? Register   Sign In
Dynamic Database Selection with Hooks
#1

[eluser]whobutsb[/eluser]
Hello All,

I'm starting to build a new iteration of my application to allow other companies use my application. One of design choices I decided to make was to separate user data in their own databases. My plan of attack is as follows:

- User Logins
- A Master User Database is Checked for correct credentials
- User Account Database Configuration is selected.
- Database Configuration stored in a hashed session (Probably a epic fail move, looking for other suggestions).
- CI Hook is run [post_controller_constructor]

Code:
function init_db(){
        //Check if the DB settings are made
        $DBSettings = $this->CI->session->userdata('DBSettings'); ;

        if(!empty($DBSettings)){
            
            $config = array(
                    'hostname'    => $DBSettings['HostAddress'],
                    'username'    => $DBSettings['Username'],
                    'password'    => $DBSettings['Password'],
                    'database'    => $DBSettings['DatabaseName'],
                    'dbdriver'    => 'mssql',
                    'dbprefix'    => '',
                    'pconnect'    => FALSE,
                    'db_debug'    => TRUE,
                    'cache_on'    => FALSE,
                    'cachedir'    => '',
                    'char_set'    => 'utf8',
                    'dbcollat'    => 'utf8_general_ci'
                );
            
            $this->CI->load->database($config);
        }
    }

The issue that is arising is I don't know how to make this database configuration that I'm loading to be the Active Group to be used in my models. I know I'm missing one function to get this to work.

Also if anyone has a better suggestion for storing the database configuration, I would love to hear it. I know my current implementation is probably not the safest way.

Thank you for your help!
#2

[eluser]srisa[/eluser]
http://ellislab.com/codeigniter/user-gui...cting.html , that should help. It contains information on connecting to database using dsns.
#3

[eluser]whobutsb[/eluser]
Thanks for the reply. I have gotten that far with configuring the database, but it doesn't seem like the once the database settings have been loaded it is considered the "Active Group" or active database, so that my Active Record queries can take advantage of the $this->db->something() calls.
#4

[eluser]whobutsb[/eluser]
So I'm trying something new. I'm including the config/database.php into my hook, and i'm setting the database configuration explicitly. And then switching the active_group in the hook. But when it comes to actually running the active record queries in the model using $this->db. It still says that i'm looking at the wrong database. Here is the hook I wrote;

Code:
<?php

class DBSettings{
    
    var $CI;
    
    function DBSettings(){
        $this->CI =& get_instance();
    }
    
    
    function init_db(){
    
        //Check if the DB settings are made
        $DBSettings = $this->CI->session->userdata('DBSettings');
        
        if(!empty($DBSettings)){
        
            require(APPPATH.'config/database.php');
            
            $db['HFDB']['hostname'] = $DBSettings['HostAddress'];
            $db['HFDB']['username'] = $DBSettings['Username'];
            $db['HFDB']['password'] = $DBSettings['Password'];
            $db['HFDB']['database'] = $DBSettings['DatabaseName'];
            $db['HFDB']['dbdriver'] = "mssql";
            $db['HFDB']['dbprefix'] = "";
            $db['HFDB']['pconnect'] = TRUE;
            $db['HFDB']['db_debug'] = TRUE;
            $db['HFDB']['cache_on'] = FALSE;
            $db['HFDB']['cachedir'] = "";
            $db['HFDB']['char_set'] = "utf8";
            $db['HFDB']['dbcollat'] = "utf8_general_ci";
            
            $active_group = "HFDB";            
        }
    }

}

Here is the config/hooks.php file:

Code:
$hook['post_controller_constructor'] = array(
    'class'        => 'DBSettings',
    'function'    => 'init_db',
    'filename'    => 'DBSettings.php',
    'filepath'    => 'hooks',
    
);

If anyone has any experience dynamically switching databases, I would love to hear your thoughts on this.




Theme © iAndrew 2016 - Forum software by © MyBB