Alexandre Bolduc
Alexandre Bolduc

Reputation: 825

CodeIgniter PHP Model Access "Unable to locate the model you have specified"

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 {

....

Upvotes: 47

Views: 190162

Answers (16)

Pravin Shinde
Pravin Shinde

Reputation: 311

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);

Upvotes: 1

Paulitos Germanos
Paulitos Germanos

Reputation: 11

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);

Upvotes: 0

Krish
Krish

Reputation: 419

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.

Upvotes: 3

theTypan
theTypan

Reputation: 5887

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

Upvotes: 0

Frank Conry
Frank Conry

Reputation: 2726

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

Upvotes: 1

Sandeep Sherpur
Sandeep Sherpur

Reputation: 2802

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

Upvotes: 1

user3642940
user3642940

Reputation: 421

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

Upvotes: 1

Gabriel Glauber
Gabriel Glauber

Reputation: 971

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.

Upvotes: 7

Atul O Holic
Atul O Holic

Reputation: 6792

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. :)

Upvotes: 3

Avnish Tiwary
Avnish Tiwary

Reputation: 2286

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');

Upvotes: 4

Suleiman Umar
Suleiman Umar

Reputation: 31

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';

Upvotes: 1

wclark
wclark

Reputation: 436

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.

Upvotes: 8

jakentus
jakentus

Reputation: 896

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

Upvotes: 66

ialbescu
ialbescu

Reputation: 161

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

Upvotes: 4

Repox
Repox

Reputation: 15475

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.

Upvotes: 72

Jonathan
Jonathan

Reputation: 326

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.

Upvotes: 2

Related Questions