2015-02-17 73 views
0

我遇到的问题不确定如何使用模型,视图和控制器进行设置。我是codigniter的新手,有点失去了学习。我在控制器中设置了一切,我知道现在是错误的,当我点击提交后插入一条记录时,页面出现页面无法显示。当它应该只是将我重定向到我插入记录。如何设置此数据以使用视图和模型

员工控制器

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

    class Employee extends CI_Controller { 
    function __construct() 
{ 
    parent::__construct(); 
    $this->load->model('employee_model'); 

    } 

    //Insert the employee 
     public function insert_employee() 
     { 


      $data=array('name'=>$this->input->post('name'), 
       'LanId'=>$this->input->post('LanId'), 
       'reason'=>$this->input->post('reason'), 
       'PepNumber'=>$this->input->post('PepNumber'), 
       'Employee_Number'=>$this->input->post('Employee_Number'), 
       'department'=>$this->input->post('department'), 

       'status'=>1); 
      //print_r($data); 

      $result=$this->employee_model->insert_employee($data); 
      if($result==true) 
      { 
       $this->session->set_flashdata('msg',"Employee Records Added Successfully"); 
       redirect('employee/index'); 

      } 
      else 
      { 

       $this->session->set_flashdata('msg1',"Employee Records Added Failed"); 
       redirect('employee/index'); 


      } 
     } 

员工模型

<?php 

class Employee_model extends CI_Model 
{ 

    public function insert_employee($data) 
    { 
     $this->db->insert('employee_list',$data); 
     return ($this->db->affected_rows() != 1) ? false:true; 
    } 
    public function get_employee() 
    { 
     $this->db->select('*'); 
     $this->db->from('employee_list'); 
     $this->db->where('status',1); 

     $query =$this->db->get(); 
     return $query->result(); 
    } 
    public function delete_employee($id,$data) 
    { 
     $this->db->where('id',$id); 
     $this->db->update('employee_list',$data); 
     return ($this->db->affected_rows() != 1) ? false:true; 
    } 
    public function edit_employee($id) 
    { 
     $this->db->select('*'); 
     $this->db->from('employee_list'); 
     $this->db->where('id',$id); 
     $this->db->where('status',1); 
     $query =$this->db->get(); 
     return $query->result(); 

    } 
    public function update_employee($data,$id) 
    { 
     $this->db->where('id',$id); 
     $this->db->update('employee_list',$data); 
     return ($this->db->affected_rows() != 1) ? false:true; 
    } 
} 
+0

除非你已经做了,在构造函数或自动加载,你必须在使用前加载模型:'$这个 - >负载>模型(“employee_model”);' – AdrienXL 2015-02-17 20:26:06

回答

0

在使用它之前,您没有加载模型。

有多种方式来处理模型。

如果你需要准时,你可以加载在你的函数:

public function myFunction() 
{ 
    $this->load->model("mymodel"); 
    $this->mymodel->myfunction(); 
} 

但是,如果它在你的控制器一种很常见的模式,可以为所有在构造函数加载一次:

class MyController extends CI_Controller 
{ 

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

    public function index() 
    { 
     $this->mymodel->myfunc(); 
    } 
} 

最后,如果它是应用程序的中心模型,则可以为您的所有控制器加载它。

进入的application/config/autoload.php:

$autoload['model'] = array("mymodel"); 

编辑:另外,你的模型的回归是不正确的。

return $this->db->affected_rows(); //It's enought :) 
+0

这是所有写在控制器中是不正确的 – Veronica 2015-02-17 21:07:51

+0

我没有在你提供的代码中看到'load-> model()'。 – AdrienXL 2015-02-17 21:09:04

+0

刚刚添加了模型 – Veronica 2015-02-17 21:09:50

0

与模型相关工作笨:

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

而且你打电话从MODEL_NAME类方法:

$this->Model_Name->some_method($parametar1, $parametar2);