shuimenjian 发表于 2014-11-28 13:13:53

Model 构造函数加载数据库后,其他方法仍然不能使用

本帖最后由 shuimenjian 于 2014-11-28 13:15 编辑

连接了两个数据库,在model 类的构造函数中加载数据库后,该类的其他方法仍然无法使用加载后的数据库。在方法get_local_data()和get_remote_data()中,无法使用$db_local和$db_remote,提示non-object。数据库配置无任何问题,在两个方法里面调用后,都是正常的,请问这个问题怎么解决呀? 代码如下:


class M_db_sync extends CI_Model {

      function __construct(){
      parent::__construct();
               
      $db_local = $this->load->database('default', TRUE);      
      $db_remote = $this->load->database('remote', TRUE);      
    }

    function get_local_data(){
      
                // $db_local = $this->load->database('default', TRUE);      
      
                $db_local->order_by("id", "desc");
                $db_local->limit(5);
                $query = $db_local->get('test');
               
                return $query;
      }
      
      function get_remote_data(){
      
                //$db_remote = $this->load->database('remote', TRUE);      

                $db_remote->limit(10);
                $query = $db_remote->get('Current_Full');
               
                return $query;
      }
      

}





shuimenjian 发表于 2014-11-29 14:36:49

额 谁知道 帮忙解决下巴

shuimenjian 发表于 2014-11-29 19:01:33

问题已解决,给变量加上$this 就OK 了
页: [1]
查看完整版本: Model 构造函数加载数据库后,其他方法仍然不能使用