0

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

1

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

4 Comments

tried doing this but the same error still appears. i kinda fixed it by repeating the line $cms_db = $this->load->database('cms', TRUE); and the other one in each function inside my site model but this method seems too tedious. also thanks for the answer
check edited version MY_Model no need to repeat in every mothed you can call $this->cms_db->select('*')->get_where('yourtable')->result();
tried adding the lines in the MY_Model and this error appeared Severity: Notice Message: Undefined property: Site::$site_model Filename: core/Model.php as well as A PHP Error was encountered Severity: Notice Message: Indirect modification of overloaded property Site_model::$site_model has no effect Filename: models/site_model.php
you have to put MY_Model in application/core directory and extend your models in normal way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.