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

[控制器] CI加载不到model层 这是少了什么配置?我的CI版本是2.1.4的

[复制链接]
发表于 2013-11-6 11:12:53 | 显示全部楼层 |阅读模式
model层:
class News_model extends CI_Model
{
       
       
        public function __construct(){
                parent::__construct();
                $this->load->database();
        }
       
       
       
        public function take_news()
        {
                $query=$this->db->get('news');
                return $query->row_array();
        }
}


控制器:
class Welcome extends CI_Controller {

        /**
         * Index Page for this controller.
         *
         * Maps to the following URL
         *                 http://example.com/index.php/welcome
         *        - or -  
         *                 http://example.com/index.php/welcome/index
         *        - or -
         * Since this controller is set as the default controller in
         * config/routes.php, it's displayed at http://example.com/
         *
         * So any other public methods not prefixed with an underscore will
         * map to /index.php/welcome/<method_name>
         * @see http://codeigniter.com/user_guide/general/urls.html
         */
       
        public  function __construct()
        {
                $this->load->model('News_model');
        }
       
        public function index()
        {
                $data['a'] = '变量测试输出';
            $data['b'] = '案例拉倒辣的了卡的林科大辽阔的';   
               
            $data=$this->news_model->take_news();
            var_dump($data);
                $this->load->view('welcome_message.html',$data);
        }
}


视图输出:
A PHP Error was encounteredSeverity: Notice
Message: Undefined property: Welcome:load
Filename: controllers/welcome.php
Line Number: 23


Fatal error: Call to a member function database() on a non-object in D:\wamp\www\htdocs\CodeIgniter\application\controllers\welcome.php on line 23



发表于 2013-11-6 11:55:00 | 显示全部楼层
控制器
PHP复制代码
 
public  function __construct()
{
     parent::__construct();
     $this->load->model('news_model');
}
复制代码
 楼主| 发表于 2013-11-6 12:06:05 | 显示全部楼层
解决了 3Q  网上版本各有不同啊  最后还是像你说的这样  谢谢
发表于 2013-11-6 20:47:56 | 显示全部楼层
大小写的问题
发表于 2013-11-6 21:15:26 | 显示全部楼层
自定义的__construct()没有调用parent的
发表于 2013-11-7 21:11:33 | 显示全部楼层
算得上进阶讨论?大开眼界!

本版积分规则