45

I have been trying to load some models for this website I am building. However, for an unknown reason, it will bring the following error :

An Error Was Encountered

Unable to locate the model you have specified: logon_model

Now , I have done my research. The problem would be that IC processes file names in lowercase. However, both my file and the file calling is in lower case, as shown here :

echo "VALIDATING";
            // Validation passed. Off we go to account info verification from AA's database. God help us all.
            $this->load->model('logon_model');
            echo "FOUND MODEL";
            $res = $this->logon_model->verify_user($this->input->post('username'),$this->input->post('password'));
            echo $this->input->post('username');
            echo $this->input->post('password');

The execution does not reach "FOUND MODEL", thus stops on the model loading. I have tried to use:

 $this->load->model(site_url('logon_model'));

With no results. Need to mention the model file is correctly placed in the right model folder ?

How can I fix this ?

EDIT : Header for the model file :

class Logon_model extends CI_Model {

....
3
  • 1
    Also, it is logon_model and not login_model, right? Otherwise, see wclark's answer
    – Kai Qing
    Nov 10, 2011 at 2:53
  • @Kai Qing It is all logon_model Nov 10, 2011 at 2:56
  • Be sure the model class and file name are same and only first letter must be uppercase e.g "Model_user"
    – Majid
    Feb 27, 2018 at 16:49

16 Answers 16

72

When creating models, you need to place the file in application/models/ and name the file in all lowercase - like logon_model.php

The logon_model.php should contain the following:

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


class Logon_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    ...

Now, what you can do, to test if your application model is reachable, is to try opening it in the browser - like so:
http://example.org/application/models/logon_model.php

If you see the text No direct script access allowed it means you hit the right file (if you are in doubt, try writing something else in the exit() in the first line).

Secondly, for loading the model in your controllers, you should be able to do like this:

public function index()
{

    $this->load->model('logon_model');
    ...

}

If everything above checks out as expected I would begin looking at file permissions and/or possibly symlinks if you are using any.

2
  • 5
    The php file name must match the class name and the first character of the class must be uppercase. Thus, you should place the Logon_model class in the Logon_model.php . The case sensitivity in the file name isn't an issue if you are on windows, but it will be on linux.
    – Jeremy
    Feb 9, 2018 at 7:15
  • 1
    For me, it works at local but on webserver(cpanel). I also needed to rename model file in UPPER case letters i.e. (Logon_model.php as per this example)
    – ramya
    Oct 2, 2019 at 9:39
66

I use codeigniter 3+. I had the same problem and in my case I changed model file name starting from uppser case.

Logon_model.php

2
  • 3
    Same. I am using Codeigniter 3.0 and I needed to name the file in the model folder from 'test_model.php' to 'Test_model_php'. Apr 23, 2015 at 19:40
  • 5
    Want to point out that the uppercase letter is only required on unix based systems such as linux and mac osx. on windows starting with un undercase letter works fine.
    – user2229108
    Sep 10, 2015 at 7:01
8

Here is what a model should look like: Make sure yours is like this.

    <?php
    class Logon_model extends CI_Model {

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

    function myFunc()
    {
      // do something
    }
}

note the upper-case class name.

To load it use:

$this->load->model('logon_model');

note all lower case.

1
  • I've added the header, check it out Nov 10, 2011 at 2:52
7

I resolve this with this way:

  1. I rename file do Page_model.php
  2. Class name to Page_model extends...
  3. I call on autoload: $autoload['model'] = array('Page_model'=>'page');

Works fine.. I hope help.

4

In CodeIgniter 3.0-dev (get it from github) this is not working because the CI is search as first letter uppercase.

You can find the code on system/core/Loader.php line 282 or bellow:

$model = ucfirst(strtolower($model));

foreach ($this->_ci_model_paths as $mod_path)
{
    if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
    {
        continue;
    }

    require_once($mod_path.'models/'.$path.$model.'.php');

    $CI->$name = new $model();
    $this->_ci_models[] = $name;
    return $this;
}

This mean that we need to create the file with the following name on application/models/Logon_mode.php

4

First letter of file name and class name must be in Uppercase.

Your model class will be

class Logon_model extends CI_Model

and the file name will be Logon_model.php

Access it from your contoller like

$this->load->model('Logon_model');

3

Adding to @jakentus answer, below is what worked for me:

  1. Change the file name in the models package to Logon_model.php (First letter upper case as @jakentus correctly said)

  2. Change the class name as same as file name i.e.

    class Logon_model extends CI_Model

  3. Change the name in the load method too as

    $this->load->model('Logon_model');

Hope this helps. Happy coding. :)

2

Models must be named and called with the first letter of the model name capitalized and the rest in lowercase.

For example: $this->load->model('Logon_model');

and:

class Logon_model extends CI_Model {
...

But you are correct about the file name.

4
  • I've edited them both like you say, and it still does not work ! Nov 10, 2011 at 3:29
  • Exactly what folder is your model in? It should be application/models/ and it should be named logon_model.php. If all of the above conditions are met, there's no reason you shouldn't be able to load it as Logon_model.
    – Jonathan
    Nov 10, 2011 at 3:34
  • Everything is correctly placed, so I do not understand either. Nov 10, 2011 at 3:39
  • rename model to Logon_model.php
    – Rashi
    Jan 9, 2014 at 18:53
2

Change your model file name, start with upper case letter like this: Logon_model.php

This happens when we migrate our project from windows server to LINUX server because linux is case sensitive while windows is not.

So, on windows even if don't write file name with upper case then also it will work fine but not on linux.

1

I experienced the same problem, but I fixed it by altering my application/config/routes.php file.

I made some restructuring to my controller directories and forget to effect it on the routes file.

Earlier:

$route['application/apply'] = 'ex/application/account/create';

and now:

$route['application/apply'] = 'application/account/create';

1

Changing the name of model name starting with Uppercase works. Example : Login_model.php instead of login_model.php

1

you must change your model name first letter capital. in localhost small letter work properly but online this not work. for exa:

common_model.php

replaced it to

Common_model.php
1

Just adding my problem i had:

$this->load->model("planning/plan_model.php");

and the .php shouldnt be there, so it should have been:

$this->load->model("planning/plan_model");

hope this helps someone

1

If you are on Linux then make sure your File name must match with the string passed in the load model methods the first argument.

$this->load->model('Order_Model','order_model');

You can use the second argument using that you can call methods from your model and it will work on Linux and windows as well

$result = $this->order_model->get_order_details($orderID);
0

Make sure:

  1. First letter uppercase
  2. Class name exact name as file name
  3. Make sure your file ends with .php extension

In my case I had 1 and 2 correct but forgot to name my file with .php extension. How I forgot, no idea but it sure gave me a hard time trying to figure out the problem

0

You can give whatever name you want.

Styles guides are recommendations and not musts.

But you have to care to use everywhere the same name.

For example for Test_Model you have to:

Class Name

        class Test_Model extends CI_Model

File Name

        Test_Model.php

Load Model

        $this->load->model('Test_Model');

Use Model

        $this->Test_Model

To avoid using hard coding strings you can load model like this:

        $this->load->model(Test_Model::class);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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