用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3077|回复: 5
收起左侧

[已解决] 在扩展类中无法使用数据库类?斑竹帮忙看一下,谢谢先

[复制链接]
发表于 2010-1-25 15:54:48 | 显示全部楼层 |阅读模式
本帖最后由 penglu3000 于 2010-1-25 15:56 编辑
PHP复制代码
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
发表于 2010-1-25 16:31:07 | 显示全部楼层
所有类库里都不允许使用控制器里的 $this
请使用 $CI =& get_instance(); $CI->load->xxxx();

这个在手册里也有说明。
 楼主| 发表于 2010-1-25 16:42:09 | 显示全部楼层
本帖最后由 penglu3000 于 2010-1-25 16:46 编辑

多谢版主指教,我根据你说的改动一下还是报错。
PHP复制代码
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
发表于 2010-1-25 17:26:07 | 显示全部楼层
我看了一下源码,Config 类里的构造函数不支持 get_instance();
请在非构造函数中使用 get_instance();

PS: 也就是说没有执行控制器代码之前,是没有 get_instance() 的,和是不是构造函数貌似关系也不大,呵呵
 楼主| 发表于 2010-1-25 17:39:10 | 显示全部楼层
好的,多谢了,我再研究研究
发表于 2010-1-26 10:17:03 | 显示全部楼层
改成乳齿:
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);
    }
}

本版积分规则