CodeIgniter Forums
Multi-Database Connection Problems with a controller and model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Multi-Database Connection Problems with a controller and model (/thread-6742.html)



Multi-Database Connection Problems with a controller and model - El Forum - 03-10-2008

[eluser]ccachor[/eluser]
I'm hoping some people can help me with a problem here. We have a tricky configuration for a new project where we're not using the database configuration from the config folder. Rather we have two functions in the controller, one for the master db and one for the secondary db. We connect to the first database to look up the connection info for the second database.

The problem occurs where we try to pass the connection info to the model. Here is a code sample:

Code:
function links() {
        $this->load->model("LinkModel","",$this->_getSecondaryDbConfig());
        $linkArr = $this->LinkModel->getAll();
        $data['links'] = $linkArr['records'];
        $this->load->links('dashboard/links',$data);

function _getSecondaryDbConfig() {
        $this->load->model("SiteModel",'',$this->_getMasterDbConfig());
        $this->SiteModel->loadByUrl($_SERVER['SERVER_NAME']);
        $config['hostname'] = $this->SiteModel->getDbhost();
        $config['username'] = $this->SiteModel->getDbuser();
        $config['password'] = $this->SiteModel->getDbpass();
        $config['database'] = $this->SiteModel->getDb();
        $config['dbdriver'] = "mysqli";
        $config['dbprefix'] = "";
        $config['pconnect'] = FALSE;
        $config['db_debug'] = TRUE;
        $config['cache_on'] = FALSE;
        $config['cachedir'] = "";
        $config['char_set'] = "utf8";
        $config['dbcollat'] = "utf8_general_ci";
        return $config;    

    function _getMasterDbConfig() {
        $config['hostname'] = "localhost";
        $config['username'] = "test";
        $config['password'] = "test";
        $config['database'] = "master";
        $config['dbdriver'] = "mysqli";
        $config['dbprefix'] = "";
        $config['pconnect'] = FALSE;
        $config['db_debug'] = TRUE;
        $config['cache_on'] = FALSE;
        $config['cachedir'] = "";
        $config['char_set'] = "utf8";
        $config['dbcollat'] = "utf8_general_ci";
        return $config;

Then the model ------

Code:
public function getAll() {
        $query = $this->db->query('SELECT count(*) as total FROM links ');
        if ($query->num_rows() > 0) {
            $row = $query->row_array();
            $total = $row['total'];
            $query = $this->db->query('SELECT * FROM links ');
            return array('total' => $total, 'records' => $query->result_array());
        } else {
            return array('total' => 0, 'records' => array());
        }
    }

The problem is that it's looking for the links table on the master database, not the secondary database. I've tried multiple solutions but haven't come up with anything using the model and controller, just the controller. Any help is greatly appreciated!


Multi-Database Connection Problems with a controller and model - El Forum - 03-10-2008

[eluser]rvent[/eluser]
You might need to define additional databases in the "database" file in your config folder...