Welcome Guest, Not a member yet? Register   Sign In
Multiple Database problem
#1

[eluser]Unknown[/eluser]
I'm using multiple databases.

Code:
$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = $_SERVER['DB_HOST_1'];
$db['default']['username'] = $_SERVER['DB_USER_1'];
$db['default']['password'] = $_SERVER['DB_PASS_1'];
$db['default']['database'] = $_SERVER['DB_NAME_1'];
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$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['webhaven']['hostname'] = $_SERVER['DB_HOST_2'];
$db['webhaven']['username'] = $_SERVER['DB_USER_2'];
$db['webhaven']['password'] = $_SERVER['DB_PASS_2'];
$db['webhaven']['database'] = $_SERVER['DB_NAME_2'];
$db['webhaven']['dbdriver'] = 'mysql';
$db['webhaven']['dbprefix'] = '';
$db['webhaven']['pconnect'] = TRUE;
$db['webhaven']['db_debug'] = FALSE;
$db['webhaven']['cache_on'] = FALSE;
$db['webhaven']['cachedir'] = '';
$db['webhaven']['char_set'] = 'utf8';
$db['webhaven']['dbcollat'] = 'utf8_general_ci';
$db['webhaven']['swap_pre'] = '';
$db['webhaven']['autoinit'] = TRUE;
$db['webhaven']['stricton'] = FALSE;

$db['extraone']['hostname'] = $_SERVER['DB_HOST_3'];
$db['extraone']['username'] = $_SERVER['DB_USER_3'];
$db['extraone']['password'] = $_SERVER['DB_PASS_3'];
$db['extraone']['database'] = $_SERVER['DB_NAME_3'];
$db['extraone']['dbdriver'] = 'mysql';
$db['extraone']['dbprefix'] = '';
$db['extraone']['pconnect'] = TRUE;
$db['extraone']['db_debug'] = FALSE;
$db['extraone']['cache_on'] = FALSE;
$db['extraone']['cachedir'] = '';
$db['extraone']['char_set'] = 'utf8';
$db['extraone']['dbcollat'] = 'utf8_general_ci';
$db['extraone']['swap_pre'] = '';
$db['extraone']['autoinit'] = TRUE;
$db['extraone']['stricton'] = FALSE;

It is works fine until I'm not mixing in one model. But as soon in the same model one function using active record on the second (not default) database:

Code:
public function getuser()
{
        
        $DB2 = $this->load->database('webhaven', TRUE);    
            
        $param = isset($_GET['term']) ? $_GET['term'] : '';
        $term = $param;
    
        $qry = "SELECT * FROM users WHERE gecos REGEXP '^$param'";
        
        $rows = $DB2->query($qry);
...
}

and the next public function would use the default one, the CI database layer is stuck, and although I'm calling like this:
Code:
public function feedback_delegate($delegated_to = '')
{
...

        $this->db->where('id', $id);
        $this->db->update('feedback', $data);
...
}

still fails, as trying to use the previous ($DB2) link, and obviously can't find the table.
My workaround is always calling the right database at the beginning of the function.

Code:
public function feedback_delegate($delegated_to = '')
{

$this->load->database('default', TRUE);
...

        $this->db->where('id', $id);
        $this->db->update('feedback', $data);
...
}


This way everything is works fine. BUT.

Today I've tried to change the session handling from the default cookie system to the database driven version.
The Session handler just not worked. I've debugged and I've found, that (in /library/Session.php ) the function sess_create() worked, but the next one function sess_write() failed and can't update the ci_sessions table. After some struggling I've found, that root of the problem again about the multiple databases, and if I'm adding

Code:
$this->CI->load->database('default', TRUE);

line to the top of the function, everything works fine.

Guess, this is a bug related to the Database library, but so far I haven't had time to check trough where is the point where the CI loose the track.




Theme © iAndrew 2016 - Forum software by © MyBB