Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

CodeIgniter 3 loads the model but the property is undefined

ErrorUnable to locate the model you have specified: User

Short answer

CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model. On Linux a lowercase user.php is not found. A model name that matches an existing controller property is also refused. Rename the file and class, or load the model with a second alias argument.

  • Applies to CodeIgniter 3.1, PHP 7.4, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the models folder and compare the exact spelling and case of the file with the class name inside it and with the name you pass to load->model().

    ls application/models/
    grep -n "^class" application/models/*.php
  2. 2Match what you see, then jump to its fix

    1. 1Unable to locate the model you have specifiedThe file name does not match the class nameFix
    2. 2The model name you are loading is the name of a resource that is already being usedThe model name clashes with something already on the controllerFix

What does the error look like?

  • Unable to locate the model you have specified: User
  • The model name you are loading is the name of a resource that is already being used: user

Cause 1 of 2

The file name does not match the class name

1Check

The user guide says the file name must match the class name, and the class name has the first letter capitalised. The loader looks for the file with the first letter capitalised too, so a lowercase file such as user.php is missed on a case-sensitive system like Linux.

ls application/models/ | grep -i user

2Fix

Name the file after the class, with a capital first letter, and extend CI_Model.

// application/models/User_model.php
class User_model extends CI_Model
{
    public function get_all()
    {
        return $this->db->get('users')->result();
    }
}

3Verify

Load it in a controller and call a method.

$this->load->model('user_model');
var_dump($this->user_model->get_all());

Confirmed by codeigniter.com

Did this fix it?

Cause 2 of 2

The model name clashes with something already on the controller

1Check

The loader refuses to attach a model when the controller already has a property with that name, for example a library or another model loaded earlier under the same name.

grep -rn -E "load->(model|library)\('user" application/

2Fix

Give the model a distinct name, or pass a second argument to load->model() to choose the property it is stored under.

$this->load->model('user_model', 'account');
$rows = $this->account->get_all();

3Verify

The call through the alias works and the clash error is gone.

var_dump(isset($this->account));

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Name every model with a _model suffix so it never matches a controller, library or table name.
  • Develop on a case-sensitive system, or match case exactly, since many servers are Linux.
  • Load models in the controller constructor or autoload, and call them only through the property you chose.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

CodeIgniter 4 cannot write its cache files

CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/

CodeIgniter 4's file cache needs a cache directory that the web server can really write to.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.