CodeIgniter 3 loads the model but the property is undefined
Unable to locate the model you have specified: UserShort 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.
Fix it fast
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/*.php2Match what you see, then jump to its fix
What does the error look like?
Unable to locate the model you have specified: UserThe 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 user2Fix
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
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
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
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack 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 fixBack end
CodeIgniter 3 stops with "Your system folder path does not appear to be set correctly"
503 Service Unavailable
CodeIgniter 3's front controller resolves $system_path and then checks that the result is a directory.
3 causes, each with a fixBack end
Composer cannot reach repo.packagist.org on port 443
Operation timed out after 300000 milliseconds
When Composer cannot reach repo.packagist.org, run composer diagnose first; it tests the connection and the certificate store.
4 causes, each with a fix