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

[模型] 学习ci,按教程编写,无法加载模型中的方法

[复制链接]
发表于 2018-2-2 12:03:47 | 显示全部楼层 |阅读模式
2CI币
<?php
class News_model extends CI_Model {

    public function __construct()
    {
        $this->load->database();
    }


        public function get_news($slug = FALSE)
        {
            if ($slug === FALSE)
            {
                $query = $this->db->get('news');
                return $query->result_array();
            }

            $query = $this->db->get_where('news', array('slug' => $slug));
            return $query->row_array();
        }

        public function set_news()
        {
            $this->load->helper('url');

            $slug = url_title($this->input->post('title'), 'dash', TRUE);

            $data = array(
                'title' => $this->input->post('title'),
                'slug' => $slug,
                'text' => $this->input->post('text')
            );

            return $this->db->insert('news', $data);
        }
}
?>


A PHP Error was encountered
Severity: Notice
Message: Undefined property: News:news_model
Filename: controllers/News.php
Line Number: 13
Backtrace:
File: /Users/laijh/Sites/CI_practise/application/controllers/News.php
Line: 13
Function: _error_handler       
File: /Users/laijh/Sites/index.php
Line: 315
Function: require_once

An uncaught Exception was encountered
Type: Error
Message: Call to a member function get_news() on null
Filename: /Users/laijh/Sites/CI_practise/application/controllers/News.php
Line Number: 13
Backtrace:
File: /Users/laijh/Sites/index.php
Line: 315
Function: require_once



 楼主| 发表于 2018-2-2 12:04:27 | 显示全部楼层
贴上controller
回复

使用道具 举报

 楼主| 发表于 2018-2-2 12:04:47 | 显示全部楼层
<?php
class News extends CI_controller{

        public function __contruct()
        {
                parent::__construct();
                $this->load->model('News_model');
                $this->load->model('url_helper');
        }

        public function index()
        {
                $data['news'] = $this->news_model->get_news();
                $data['title']='News archive';

                $this->load->view('templates/header',$data);
                $this->load->view('news/index',$data);
                $this->load->view('templates/footer',$data);
        }

        public function view($slug=NULL)
        {
                $data['news_item'] = $this->news_model->get_news($slug);

                if(empty($data['news_item']))
                {
                        show_404();
                }

                $data['title']=$data['news_item']['title'];

                $this->load->view('templates/header',$data);
                $this->load->view('news/index',$data);
                $this->load->view('templates/footer');
        }

        public function create()
        {
            $this->load->helper('form');
            $this->load->library('form_validation');

            $data['title'] = 'Create a news item';

            $this->form_validation->set_rules('title', 'Title', 'required');
            $this->form_validation->set_rules('text', 'Text', 'required');

            if ($this->form_validation->run() === FALSE)
            {
                $this->load->view('templates/header', $data);
                $this->load->view('news/create');
                $this->load->view('templates/footer');
            }
            else
            {
                $this->news_model->set_news();
                $this->load->view('news/success');
            }
        }
}
?>
回复

使用道具 举报

 楼主| 发表于 2018-2-2 12:05:23 | 显示全部楼层
URL   http://localhost/index.php/news/index
回复

使用道具 举报

 楼主| 发表于 2018-2-2 12:09:34 | 显示全部楼层
求帮忙啊!卡了很久了!!!!!!!!试过换版本
回复

使用道具 举报

 楼主| 发表于 2018-2-2 12:15:50 | 显示全部楼层
没人吗!this->model_name->method()加载不了
回复

使用道具 举报

 楼主| 发表于 2018-2-2 12:32:37 | 显示全部楼层
应该是Ci的model配置问题!下载后没有改过除index.php,和config.PHP,database.php页面!!!!!求大佬帮助!!!!!
回复

使用道具 举报

 楼主| 发表于 2018-2-2 12:36:53 | 显示全部楼层
还有是可以加载表单view的,$this->load->model_name从报错上也是可以看出可以加载的
回复

使用道具 举报

发表于 2018-2-3 11:01:55 | 显示全部楼层
Ricardo 发表于 2018-2-2 12:36
还有是可以加载表单view的,$this->load->model_name从报错上也是可以看出可以加载的 ...

从你的错误信息看  明明是没有找到model, 首先你的model是否有放在子目录中 ,放在子目录要加上目录名,其次试试model取个别名再用 我也刚学.勿喷
回复

使用道具 举报

发表于 2018-2-3 14:29:32 | 显示全部楼层
注意大小写,一个是 news_model 一个是 News_model
回复

使用道具 举报

本版积分规则