~夜行侠~ 发表于 2011-9-24 10:08:38

关于数据库的autoload

1.database.php 中的参数已经设置好。
2.在application\config\autoload.php 55行设置了: $autoload['libraries'] = array('database');
3.创建testController.php
<?php
class TestController extends CI_Controller{
    function __construct(){
      parent::__construct();
    }
    function index(){
      echo 'ok';
    }
    function callModel(){
      $this->load->model('Testmode');
         $this->Testmode->result();
    }
   
}
4.创建testmode.php
<?php
class Testmode extends CI_Model{
    function __construct(){
      parent::__construct();
    }
    function result(){
      print_r($this->db);
    }
   
}
5.浏览器打开http://localhost/web/goda/index.php/testController/callModel


错误提示:
A PHP Error was encounteredSeverity: Notice
Message:Undefined property: TestController::$db
Filename: core/Model.php
Line Number: 50


解决方法:
修改以上第4步:
<?php
class Testmode extends CI_Model{
    function __construct(){
      parent::__construct();
    }
    function result(){
      $this->load->database();//手动加载数据库
      print_r($this->db);
    }
   
}

只有有这一句手动加载:$this->load->database();程序就正常了,不管 $autoload['libraries'] = array('database');或 $autoload['libraries'] = array();
CI版本:2.0.3
问题:
1.看其他许多帖子关于这个问题都是关于有没有开启自动加载,为什么这里这个自动加载没有用?
2.$autoload['libraries'] = array();不是加载libraries文件夹下面的类吗?这里面并没有Database.php啊?


~夜行侠~ 发表于 2011-9-24 10:10:19

期待各位的回答,本人刚刚学习CodeIgniter框架。

yuzhigang5460 发表于 2011-9-24 18:00:07

~夜行侠~ 发表于 2011-9-24 10:10 static/image/common/back.gif
期待各位的回答,本人刚刚学习CodeIgniter框架。

按照以上的代码设置不会出现问题,
我在本地做了测试,没有你出现的问题。重新复制你的代码新建一个项目试试;

database的类库不在libraries下面,在database下
页: [1]
查看完整版本: 关于数据库的autoload