[eluser]Unknown[/eluser]
I'm having trouble connecting to multiple databases.
A controller requesting 4 models, each of these models use a different database.
Code controller making the call to the models
Code:
if($this->input->post('database1'))
{
$this->load->model("systems/Database1_Model","database1Model");
$this->database1Model->insertUser($name,$email,$login,$pass);
$this->model->updateAccessSystem($userId,$this->input->post('database1'),true);
}
if($this->input->post('database2'))
{
$this->load->model("systems/Database2_Model","database2Model");
$this->database2Model->insertUser($name,$email,$login,$pass);
$this->model->updateAccessSystem($userId,$this->input->post('database2'),true);
}
if($this->input->post('database3'))
{
$this->load->model("systems/Database3_Model","database3Model");
$this->database3Model->insertUser($name,$email,$login,$pass);
$this->model->updateAccessSystem($userId,$this->input->post('database3'),true);
}
if($this->input->post('database4'))
{
$this->load->model("systems/Database4_Model","database4Model");
$this->database4Model->insertUser($name,$email,$login,$pass);
$this->model->updateAccessSystem($userId,$this->input->post('database4'),true);
}
Code example of one of the models dynamic connections
Code:
/**
* Insert user
*
* @access public
* @since Wed Aug 13 16:15:37 BRT 2008
* @param String $name Name of person.
* @param String $email Email of e-mail.
* @param String $login Login of user.
* @param String $pass Password of user.
* @return void
*/
function insertUser($name,$email,$login,$pass)
{
// Closing default connection
$this->db->close();
// Load config of new config database access
$config['hostname'] = "localhost";
$config['username'] = "root";
$config['password'] = "123";
$config['database'] = "mydatabase1";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$access = $this -> load -> database($config , TRUE);
//check exists
$data = array('login'=>$login);[/pre]
[b]$rs = $access -> get_where('admin', $data)->result_array();[/b][pre]
if(sizeof($rs)==0)
{
// Populating line
$data['login'] = $login;
$data['password'] = md5($pass);
$data['name'] = $name;
$data['email'] = $email;
$access-> insert('admin', $data);
}else
{
$access->update('admin',array('password' => md5($pass)),array('login'=>$login));
}
// Closing connection
$this-> db -> close();
$access -> close();
$this->load->database('default');
}
Code of model that uses the default connection
Code:
/**
* Insert access user of a outer system.
*
* @access public
* @since Fri Aug 22 10:48:27 BRT 2008
* @param String $userId Id of user.
* @param String $systemId Id of system.
* @return void
*/
function updateAccessSystem($userId,$systemId,$value=true)
{
if($value)
{
$data = array('id_sys_FK'=>$systemId,'id_user_FK'=>$userId);
$rs = $this->db->get_where('auto_user_sys', $data)->result_array();
if(sizeof($rs)==0)
{
$this->db->insert('auto_user_sys',$data);
}
}else
{
$where = array('id_sys_FK'=>$systemId,'id_user_FK'=>$userId);
$this->db->delete('auto_user_sys',$where);
}
}
I am opening and closing the connections right??
Thanks,