无法在Codeigniter中加载模型

时间:2014-02-10 09:17:28

标签: codeigniter

我有一个名为register_model.php的模型,我已经加载到我的控制器(register_controller.php)中的一个函数中。模型文件放在模型文件夹中。不过,我收到了这个错误。

遇到错误

无法找到您指定的模型:register_model

register_controller.php

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

class Register_controller extends CI_Controller 
{
    function __construct() 
    {
        parent::__construct();

    }

//    function index()
//    {
//        
//    }

    public function register() {

        if ($this->session->userdata('logged_in'))
        {
            //user is already logged in
            redirect('index.php');
        } 
        else {
            //init
            //$data['country_list']=$this->config->item('um_country_list');
             $data['username'] = '';
             $data['firstname'] = '';
            $data['lastname'] = '';
            $data['email'] = '';

           // $data['password'] = '';
            //$data['userlevel'] = '';

            //load rules
            $rules = $this->config->item('um_register_rules');
            //default msg
            $data['msg'] = $this->lang->line('um_form_msg');
            $this->load->model('register_model');
            if (isset($_POST['submit'])) {
                //the user has submitted the form
                //get the user input

                $data['username'] = $this->input->post('username');
                $data['firstname'] = $this->input->post('firstname');
                $data['lastname'] = $this->input->post('lastname');
                $data['email'] = $this->input->post('email');
                $data['password'] = $this->input->post('password');
                //$data['userlevel'] = $this->input->post('userlevel');

                $this->form_validation->set_rules($rules); //check with the rules
                if ($this->form_validation->run() === FALSE) {
                    //validation failed
                    $data['msg'] = $this->lang->line('um_form_error');
                    $this->load->view('user_register_form', $data);
                } else {
                    //validation passed
                    $dbdata = array(


                        'username' => $this->input->post('username'),
                        'firstname' => $this->input->post('firstname'),
                        'lastname' => $this->input->post('lastname'),
                        'email' => $this->input->post('email'),
                        'password' => $this->input->post('password'),
                        //'userlevel' => $this->input->post('userlevel')

                    );

                    $this->register_model->register_user($dbdata);

                    $data['msg']=$this->lang->line('um_form_activate');
                    //render the view
                    $this->load->view('um_msg', $data);
                }
            } else {
                //render the view
                $this->load->view('user_register_form', $data);
            }
        }
    }
}

?>

register_model.php

<?php

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

    class Register_model extends CI_Model {

        function __construct() {
            parent::__construct();
        }

        public function register_user($dbdata) {
            $this->db->insert('users', $dbdata);
        }

    }

    ?>

2 个答案:

答案 0 :(得分:0)

可能您应该在控制器中包含您的模型,尝试在parent::__construct

之后加入

这应该是这样的:

类注册扩展了CI_Controller {

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

或者如果您在大多数控制器中使用此模型,您可以使用自动加载包含它,请转至/config/autoload.php搜索$ autoload [&#39; model&#39;]并在合适的数组中添加您的模型< / p>

答案 1 :(得分:0)

首先在控制器中加载模型..

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

这对你有用......