【问题标题】:How to setup this data to work with a view and a model如何设置此数据以使用视图和模型
【发布时间】:2015-02-17 20:09:22
【问题描述】:

我有一些问题,不知道如何使用模型、视图和控制器进行设置。我是 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;
    }
}

【问题讨论】:

  • 除非你已经在构造函数或自动加载中这样做了,否则你必须在使用它之前加载你的模型:$this-&gt;load-&gt;model("employee_model");

标签: php sql-server codeigniter


【解决方案1】:

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

有多种方法可以处理模型。

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

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 :)

【讨论】:

  • 这都是写在控制器里的不正确
  • 我在您提供的代码中没有看到load-&gt;model()
  • 我看到了,但仍然需要在控制器中加载模型。
  • $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);我想知道这是否应该在模型中
  • 输入数据后一直显示页面无法显示
【解决方案2】:

在 codeigniter 中使用模型:

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

你从 Model_Name 类调用方法:

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

【讨论】:

    猜你喜欢
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多