Code Igniter框架中的未定义方法

时间:2015-02-03 03:11:18

标签: php codeigniter methods view model

我在CodeIgniter框架中遇到问题,

  1. 控制器

     public function tambah() {   
     $this->form_validation->set_rules('judul', 'Judul', 'required');
     $this->form_validation->set_rules('deskripsi', 'Deskripsi','required');
     $this->form_validation->set_rules('isi', 'Isi', 'required');
    
    
     if ($this->form_validation->run() === FALSE) {
          $data=array('title'=>'Menambah Berita',
                      'isi'  =>'admin/berita/tambah_berita'
                     );
           $this->load->view('admin/layout/wrapper',$data); 
     }else{
          $tag = url_title($this->input->post('judul'), 'dash', TRUE);
          $data = array(
    
                'judul'     => $this->input->post('judul'),
                'tag'       => $tag,//edited
                'deskripsi' => $this->input->post('deskripsi'),
                'isi'       => $this->input->post('isi'),
                'status'    => $this->input->post('status'),
                'id_admin'  => $this->input->post('id_admin')
            );
    $this->berita_model->tambah($data);
    redirect(base_url().'admin/berita/');
    

    } }

  2. 型号:

     public function tambah($data) {
          return $this->db->insert('lm_destination', $data);
        }
    
  3. 查看:视图文件已修复。 那些代码产生:
    致命错误:在第41行调用C:。\ application \ controllers \ admin \ file.php中的未定义方法Berita_model :: tambah()

  4. 有人可以帮我解决吗?感谢

2 个答案:

答案 0 :(得分:1)

你必须写

$this->load->model('berita_model');

使用前

$this->berita_model->tambah($data);

答案 1 :(得分:0)

我希望这会对你有所帮助

  1. 控制器

     public function tambah() {   
     $this->form_validation->set_rules('judul', 'Judul', 'required');
     $this->form_validation->set_rules('deskripsi', 'Deskripsi','required');
     $this->form_validation->set_rules('isi', 'Isi', 'required');
    
    
     if ($this->form_validation->run() === FALSE) {
          $data=array('title'=>'Menambah Berita',
                      'isi'  =>'admin/berita/tambah_berita'
                     );
           $this->load->view('admin/layout/wrapper',$data); 
     }else{
          $tag = url_title($this->input->post('judul'), 'dash', TRUE);
          $data = array(
    
                'judul'     => $this->input->post('judul'),
                'tag'       => $tag,//edited
                'deskripsi' => $this->input->post('deskripsi'),
                'isi'       => $this->input->post('isi'),
                'status'    => $this->input->post('status'),
                'id_admin'  => $this->input->post('id_admin')
            );
      $this->load->model('berita_model');
    $this->berita_model->tambah($data);
    redirect(base_url().'admin/berita/');
    

    } }

  2. 型号:

     public function tambah($data) {
           $this->db->insert('lm_destination', $data);
             return true;
        }