简体   繁体   中英

Loading Multiple Databases in CodeIgniter

im trying to build a website using codeigniter where i will be using two databases

however i seem to be having a problem in making them work

the error that i get is

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: cms_db

Filename: models/site_model.php

Fatal error: Call to a member function select() on a non-object

here is my code

controller(site.php)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site extends CI_controller {

public function __construct()
{
    parent::__construct();
    // Your own constructor code
    $this->load->model("site_model");

    $cms_db = $this->load->database('cms', TRUE);
    $site_db = $this->load->database('default', TRUE);

} 

public function index()
{
$data = $this->_initialize_data();
$this->load->vars($data);
$this->load->view('site/index');
}

public function _initialize_data()
{
    $data['cp'] = $this->site_model->get_cp();
    $data['op'] = $this->site_model->get_op();
    $data['learnfn'] = $this->site_model->get_learnfn();
    return $data;
}


}

model(site_model.php)

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    //  CI 2.0 Compatibility
    if(!class_exists('CI_Model')) { class CI_Model extends Model {} }

    class Site_model extends CI_Model
    {   
        function get_cp()
        {
            return $this->db->select()->from("listings")->where("list_UID","2")->get();
        }

        function get_op()
        {
            return $this->db->select()->from("listings",0,4)->where("list_UID","1")->get();
        }

        function get_learnfn()
        {
            return $cms_db->select()->from("education")->get()->row();
        }
    }

config(database.php)

    //CMS
    $active_group = 'cms';
    $active_record = TRUE;

    $db['cms']['hostname'] = 'localhost';
    $db['cms']['username'] = 'root';
    $db['cms']['password'] = '';
    $db['cms']['database'] = 'db2';
    $db['cms']['dbdriver'] = 'mysql';
    $db['cms']['dbprefix'] = '';
    $db['cms']['pconnect'] = TRUE;
    $db['cms']['db_debug'] = TRUE;
    $db['cms']['cache_on'] = FALSE;
    $db['cms']['cachedir'] = '';
    $db['cms']['char_set'] = 'utf8';
    $db['cms']['dbcollat'] = 'utf8_general_ci';
    $db['cms']['swap_pre'] = '';
    $db['cms']['autoinit'] = TRUE;
    $db['cms']['stricton'] = FALSE;

    //SITE
    $active_group = 'default';
    $active_record = TRUE;

    $db['default']['hostname'] = 'localhost';
    $db['default']['username'] = 'root';
    $db['default']['password'] = '';
    $db['default']['database'] = 'db1';
    $db['default']['dbdriver'] = 'mysql';
    $db['default']['dbprefix'] = '';
    $db['default']['pconnect'] = TRUE;
    $db['default']['db_debug'] = TRUE;
    $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;

and this is how i echo them out

<?php echo $learnfn->name; ?>

am i missing something?

thanks for the help

Hi first of all in config file you have to define only one active group and active record true not both and $cms_db you are loading at controller its part of controller not at module, you can do in following way if your active group is default

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site extends CI_controller {

public function __construct()
{
    parent::__construct();
    // Your own constructor code
    $this->load->model("site_model");

    $this->site_model->cms_db = $this->load->database('cms', TRUE);

} 

}

Model

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site_model extends MY_Model
{   

    public $cms_db;

    //other option if you are not passing through controller
    // this will always load cms_db 
    function __ construct(){
        parent::__construct();
     }

    function get_cp()
    {
        return $this->db->select()->from("listings")->where("list_UID","2")->get();
    }

    function get_op()
    {
        return $this->db->select()->from("listings",0,4)->where("list_UID","1")->get();
    }

    function get_learnfn()
    {
        return $this->cms_db->select()->from("education")->get()->row();
    }
}

MY_Model

//  CI 2.0 Compatibility
if(!class_exists('CI_Model')) { class CI_Model extends Model {} }

     class MY_Model extend CI_Model{
       public $cms_db = null;
        function __construct(){
           $this->site_model->cms_db = $this->load->database('cms', TRUE);
        }
     }

if you want $cms_db always create MY_Model put in its constructor load database and extend your all models with MY_Model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM