3

I'm getting an unable to locate model error.

$this->load->model('1/Gift_model');

My model file name is gift_model.php within /models/1/.

I declare the model the following way.

class Gift_model extends CI_Model {

According to CodeIgniter's documentation I'm doing it the correct way. Any suggestions? I have 5 other models named exactly the same way and they're all loading fine.

5
  • I don't think that codeigniter allows folders like 1 in it's structure.
    – u_mulder
    Sep 18, 2013 at 21:07
  • Codeigniter lets you put models within folders inside of models. ellislab.com/codeigniter/user-guide/general/models.html
    – wwwroth
    Sep 18, 2013 at 21:15
  • 1
    Correct. "If your model is located in a sub-folder, include the relative path from your models folder". '1' is my sub-folder. I have numerous other models with the same structure that load in other portions of the platform.
    – wwwroth
    Sep 18, 2013 at 21:18
  • Aaron is correct. $this->load->model('1/gift_model'); And Marcel is right - a capital letter on the file name will sometimes work like he is saying - BUT not across all platforms. so just do lower case for everything - except the first letter of a model or class. and hey while you are reading this - having a model folder named "1" - you might want to reconsider that.
    – cartalot
    Sep 19, 2013 at 0:40

9 Answers 9

13
  1. Make the model class name Uppercase My_model
  2. Make the model php file name Lowercase my_model
  3. Load the model using Lowercase (file name) $this->load->model('my_model');
1
  • 1
    2. fixed it for me Apparently it matters on certain environments that the PHP file name itself is uppercased. Very funny. Jun 28, 2018 at 20:38
3

$this->load->model('1/Gift_model'); should be $this->load->model('1/gift_model');. Lowercase on this load argument and the php filename, uppercase on the class name within the file (you had two of three correct).

2

http://www.codeigniter.com/userguide3/installation/upgrade_300.html

Starting with CodeIgniter 3.0, all class filenames (libraries, drivers, controllers and models) must be named in a Ucfirst-like manner or in other words - they must start with a capital letter.

Used to be the model files started with lower case, but they changed it.

2

Ensure that the the model name is Gift_model and the class name is also Gift_model

class Gift_model extends CI_Model
{

}

but loading the class is '1/gift_model' NOT 'Gift_model'

$this->load->model('1/gift_model');

hope this was helpful

0

The problem is that your file name is all lowercase (gift_model.php) while you are loading Gift_model within CodeIgniter. Either change the file name to Gift_model.php or update your code accordingly.

3
  • 1
    According to the codeigniter doc the file name is supposed to be lowercase and you declare it uppercase and call it uppercase. ellislab.com/codeigniter/user-guide/general/models.html
    – wwwroth
    Sep 18, 2013 at 21:14
  • Marcel you are correct but it does not work on all platforms so its best to just do lower case.
    – cartalot
    Sep 19, 2013 at 0:42
  • @PhillipJamesRoth The class should be defined with the first letter being uppercase. However, as the docs say: "The file name will be a lower case version of your class name." Similarly, the load function should load via the case of the file name, not the class name.
    – John M.
    Sep 20, 2013 at 5:35
0

Are you calling the parent's constructor for the model?

class Gift_model extends CI_Model {
    function __construct()
    {
        parent::__construct();
    }
0
-> Model Class name must be Uppercase
-> Model PHP file name must be Lowercase
-> Load Model using Lowercase(filename): $this->load->model('gift_model', TRUE);
0

Starting with CodeIgniter 3.0, all class filenames (libraries, drivers, controllers and models) must be named in a Ucfirst-like manner or in other words - they must start with a capital letter.

(Source: CI docs)

-1

`if using codeignitor 3.1.3
every thing same otherwise showing error class name => My_model

file name => My_model.php

load model => $this->load->model('My_model');

call function => $this->My_model->function();`

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.