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');
Question
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
Solution
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');
OTHER TIPS
Try to change the name of your model class from locations_model to Locations_model. And use $this->load->model('module_name'); only in the controller or method that need this model.
Firstly change the name of your model classs name locations_model to Locations_model. And use codiegnieter method for loading model in controller.
$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);
}
}
?>