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

[HELP] 遇到这样一个错误,我是ci新手,望高手给与解答,谢谢

[复制链接]
发表于 2013-3-13 12:37:31 | 显示全部楼层 |阅读模式
Fatal error: Call to undefined method News_model::set_news() in /mnt/hgfs/workspace/test/application/controllers/news.php on line 54
谷歌翻译我还是懂的  但是请看下面代码  请问哪里出了错  为什么没被定义呢
致命错误:调用未定义的方法News_model:: set_news()在第54行到/ mnt /生长因子,/工作区/测试/应用程序/控制器/ news.php上
<?php
class News extends CI_Controller {

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

  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');
  }

  public function view($slug)
{
  $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/view', $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');
  }
}
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);
}
}

发表于 2013-3-13 13:40:54 | 显示全部楼层
人家都说了你的news_model里没有定义set_news方法。。。。。。而你的set_news我看了定义在当前类里,那就用$this->set_news()吧。。。/
发表于 2013-3-13 13:46:49 | 显示全部楼层
你使用 $this->news_model->set_news() ,但是你沒加載 $this->load->model('news_model')

而且你的 news_model 在同個class,可以直接用 $this->set_news()

本版积分规则