0

我正在尝试使用 codeigniter 构建一个网站,我将在其中使用两个数据库

但是我似乎在使它们工作时遇到了问题

我得到的错误是

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: cms_db

Filename: models/site_model.php

致命错误:在非对象上调用成员函数 select()

这是我的代码

控制器(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;
}


}

模型(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();
        }
    }

配置(数据库.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;

这就是我回应它们的方式

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

我错过了什么吗?

谢谢您的帮助

4

1 回答 1

0

嗨,首先在配置文件中,您必须只定义一个活动组,并且活动记录不是两者都为真,并且 $cms_db 您正在控制器加载它的控制器部分而不是模块,如果您的活动组是默认的,您可以按照以下方式进行

<?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);

} 

}

模型

<?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_型号

//  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);
        }
     }

如果你想要 $cms_db 总是创建 MY_Model 放在它的构造函数加载数据库中,并用 MY_Model 扩展你的所有模型

于 2013-01-30T06:24:52.677 回答