我试图使用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();
}
}
配置(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;
这就是我如何回应它们
<?php echo $learnfn->name; ?>
我错过了什么吗?
感谢您的帮助
答案 0 :(得分:0)
首先,在配置文件中,您必须只定义一个活动组,并且活动记录为true,而不是两个和$ 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_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);
}
}
如果你想要$ cms_db总是在其构造函数load数据库中创建MY_Model并使用MY_Model扩展你的所有模型