如何将CodeIgniter HMVC中的模型加载到自动加载?

时间:2013-11-26 07:56:05

标签: php codeigniter hmvc

大家好,这里需要一些帮助。我正在尝试使用HMVC方法在CodeIgniter中研究和创建CRUD,但是我在加载模型时遇到了困难。这是我的结构:

CRUD

   - application
     - config
        - autoload.php
     - modules_code
        - address
          - controllers
          - models
          - views
        - locations
          - controllers
            - locations.php
          - models
            - locations_model.php
          - views
        - member
          - controllers
            - member.php
          - models
          - views
   - css
   - js
   - third_party

我的问题是我无法在配置文件夹下的autoload.php中自动加载模型 这是自动加载代码:

$autoload['model'] = array('locations_model');

在我的locations_model.php中:

<?php

    class locations_model extends CI_Model{

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

        public function getAllCountries(){

            $sql = "SELECT code,country FROM cscart_country_descriptions";
            $result = $this->db->query($sql);

            return json_encode($result->result_array());

        }

    }

?>

在我的控制器中我有这个:

<?php

    class Member extends MX_Controller{

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

        public function index(){

            $data['title'] = "Welcome to HHAlliance Member Point!";
            $data['project_title'] = "HH-Alliance - Login Page";

            $this->load->view('common/header',$data);
            $this->load->view('index',$data);
            $this->load->view('common/footer',$data);

        }

        public function authUser(){





        }

        public function register(){

            $data['title'] = "HHAlliance - User Account Registration";
            $data['project_title'] = "HH-Alliance Member Registration";
            $data['countries'] = $this->locations_model->getAllCountries();

            $this->load->view('common/header',$data);
            $this->load->view('user_register');
            $this->load->view('common/footer',$data);

        }


    }

?>

这是我的错误:

An Error Was Encountered

Unable to locate the model you have specified: locations_model

3 个答案:

答案 0 :(得分:3)

首先将您的模型类名称更改为

class Locations_model extends CI_Model{
   ....
}

类名必须首字母大写,其余名称小写。

然后,

$autoload['models'] = array('locations_model');

答案 1 :(得分:0)

尝试将您的模型类的名称从locations_model更改为Locations_model。 并使用$ this-&gt; load-&gt; model('module_name');仅在需要此模型的控制器或方法中。

答案 2 :(得分:0)

首先将模型类的名称location_model更改为Locations_model。 并使用codiegnieter方法在控制器中加载模型。

$this->load->model('model_name','model_alias_name');


<?php

    class Member extends MX_Controller{

        public function __construct(){
            parent::__construct();
            $this->load->model('locations_model','locations');

        }

        public function index(){

            $data['title'] = "Welcome to HHAlliance Member Point!";
            $data['project_title'] = "HH-Alliance - Login Page";

            $this->load->view('common/header',$data);
            $this->load->view('index',$data);
            $this->load->view('common/footer',$data);

        }

        public function authUser(){





        }

        public function register(){

            $data['title'] = "HHAlliance - User Account Registration";
            $data['project_title'] = "HH-Alliance Member Registration";
            $data['countries'] = $this->locations->getAllCountries();

            $this->load->view('common/header',$data);
            $this->load->view('user_register');
            $this->load->view('common/footer',$data);

        }


    }

?>