Jerielle Jerielle - 2 months ago 10
PHP Question

How to load a model in CodeIgniter HMVC into an autoload?

Hello guys just need a little help here. I am trying to study and create a CRUD in CodeIgniter with HMVC approach but I have a difficulty in loading the model. Here's my structure:

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


My problem is I can't autoload the model in autoload.php which is under config folder
Here's the autoload code:

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


Inside my 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());

}

}

?>


In my controller I have this:

<?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);

}


}

?>


Here's my error:

An Error Was Encountered

Unable to locate the model you have specified: locations_model

Answer

First change your model class name to

class Locations_model extends CI_Model{
   ....
}

Class names must have the first letter capitalized with the rest of the name lowercase.

then,

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