Ricardo 发表于 2018-2-2 12:03:47

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

<?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 encounteredSeverity: NoticeMessage: Undefined property: News::$news_modelFilename: controllers/News.phpLine Number: 13Backtrace: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 encounteredType: ErrorMessage: Call to a member function get_news() on nullFilename: /Users/laijh/Sites/CI_practise/application/controllers/News.phpLine Number: 13Backtrace:File: /Users/laijh/Sites/index.php
Line: 315
Function: require_once


Ricardo 发表于 2018-2-2 12:04:27

贴上controller

Ricardo 发表于 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');
          }
        }
}
?>

Ricardo 发表于 2018-2-2 12:05:23

URL   http://localhost/index.php/news/index

Ricardo 发表于 2018-2-2 12:09:34

求帮忙啊!卡了很久了!!!!!!!!试过换版本

Ricardo 发表于 2018-2-2 12:15:50

没人吗!this->model_name->method()加载不了

Ricardo 发表于 2018-2-2 12:32:37

应该是Ci的model配置问题!下载后没有改过除index.php,和config.PHP,database.php页面!!!!!求大佬帮助!!!!!

Ricardo 发表于 2018-2-2 12:36:53

还有是可以加载表单view的,$this->load->model_name从报错上也是可以看出可以加载的

my_beginner 发表于 2018-2-3 11:01:55

Ricardo 发表于 2018-2-2 12:36
还有是可以加载表单view的,$this->load->model_name从报错上也是可以看出可以加载的 ...

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

Michael锐生 发表于 2018-2-3 14:29:32

注意大小写,一个是 news_model 一个是 News_model
页: [1] 2
查看完整版本: 学习ci,按教程编写,无法加载模型中的方法