在扩展类中无法使用数据库类?斑竹帮忙看一下,谢谢先
本帖最后由 penglu3000 于 2010-1-25 15:56 编辑class MY_Config extends CI_Config{
function My_Config()
{
parent::CI_Config();
$this->load->database();
}
function db_item($name){
$this->db->select($value);
$this->db->from('config');
$this->db->where('name',$name);
$query = $this->db->get();
if($row = $query->row_array()){
return $row['value'];
}else{
return FALSE;
}
}
function db_set($name,$value){
$data = array(
'value' => $value
);
$this->db->where('name', $name);
$this->db->update('config', $data);
}
}
报错:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: MY_Config::$load
Filename: libraries/MY_Config.php
Line Number: 6
Fatal error: Call to a member function database() on a non-object in D:\workspace\store\system\application\libraries\MY_Config.php on line 6 所有类库里都不允许使用控制器里的 $this
请使用 $CI =& get_instance(); $CI->load->xxxx();
这个在手册里也有说明。 本帖最后由 penglu3000 于 2010-1-25 16:46 编辑
多谢版主指教,我根据你说的改动一下还是报错。
class MY_Config extends CI_Config{
function My_Config()
{
parent::CI_Config();
$this->ci =& get_instance();
$this->ci->load->database();
}
function db_item($name){
$this->ci->db->select($value);
$this->ci->db->from('config');
$this->ci->db->where('name',$name);
$query = $this->ci->db->get();
if($row = $query->row_array()){
return $row['value'];
}else{
return FALSE;
}
}
function db_set($name,$value){
$data = array(
'value' => $value
);
$this->ci->db->where('name', $name);
$this->ci->db->update('config', $data);
}
}
报错:
Fatal error: Call to undefined function get_instance() in D:\workspace\store\system\application\libraries\MY_Config.php on line 6 我看了一下源码,Config 类里的构造函数不支持 get_instance();
请在非构造函数中使用 get_instance();
PS: 也就是说没有执行控制器代码之前,是没有 get_instance() 的,和是不是构造函数貌似关系也不大,呵呵 好的,多谢了,我再研究研究 改成乳齿:
class MY_Config extends CI_Config{
function _load()
{
if(isset($this->ci))return;
$this->ci =& get_instance();
$this->ci->load->database();
}
function db_item($name){
$this->_load();
$this->ci->db->select($value);
$this->ci->db->from('config');
$this->ci->db->where('name',$name);
$query = $this->ci->db->get();
if($row = $query->row_array()){
return $row['value'];
}else{
return FALSE;
}
}
function db_set($name,$value){
$this->_load();
$data = array(
'value' => $value
);
$this->ci->db->where('name', $name);
$this->ci->db->update('config', $data);
}
}
页:
[1]