CodeIgniter PHP 模型访问“无法找到您指定的模型”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8074368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:54:39  来源:igfitidea点击:

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

phpmodelcodeigniter-2

提问by Alexandre Bolduc

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 :

现在,我已经完成了我的研究。问题在于 IC 以小写形式处理文件名。但是,我的文件和文件调用都是小写的,如下所示:

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 notreach "FOUND MODEL", thus stops on the model loading. I have tried to use:

执行达到“FOUND MODEL”,因此停止模型加载。我曾尝试使用:

 $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 {

....

回答by Repox

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

创建模型时,您需要将文件放入application/models/并以全部小写命名文件 - 如logon_model.php

The logon_model.phpshould contain the following:

logon_model.php应包含以下内容:

<?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

现在,您可以做的是测试您的应用程序模型是否可访问,是尝试在浏览器中打开它 - 像这样:http:
//example.org/application/models/logon_model.php

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

如果您看到文本“不允许直接脚本访问”,则表示您找到了正确的文件(如果您有疑问,请尝试在第一行的 exit() 中编写其他内容)。

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.

如果以上所有内容都按预期进行检查,我将开始查看文件权限和/或可能的符号链接(如果您正在使用)。

回答by jakentus

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

我使用 codeigniter 3+。我遇到了同样的问题,在我的情况下,我从大写字母开始更改了模型文件名。

Logon_model.php

登录模型.php

回答by wclark

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.

注意所有小写。

回答by Gabriel Glauber

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');
  1. 我重命名文件做 Page_model.php
  2. 类名到 Page_model 扩展...
  3. 我调用自动加载: $autoload['model'] = array('Page_model'=>'page');

Works fine.. I hope help.

工作正常.. 我希望有所帮助。

回答by ialbescu

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

在 CodeIgniter 3.0-dev(从 gi​​thub 获取)中,这不起作用,因为 CI 搜索为第一个字母大写。

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

你可以在 system/core/Loader.php 第 282 行或下面找到代码:

$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

这意味着我们需要在 application/models/Logon_mode.php 上创建具有以下名称的文件

回答by Avnish alok

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

文件名和类名的首字母必须大写。

Your model class will be

你的模型类将是

class Logon_model extends CI_Model

类 Logon_model 扩展了 CI_Model

and the file name will be Logon_model.php

文件名将是Logon_model.php

Access it from your contoller like

从您的控制器访问它

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

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

回答by Atul O Holic

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

添加到@jakentus 答案,以下是对我有用的内容:

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

  1. 将模型包中的文件名更改为Logon_model.php(@jakentus 正确说的第一个字母大写)

  2. 将类名更改为与文件名相同,即

    class Logon_model extends CI_Model

  3. 将加载方法中的名称也更改为

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

Hope this helps. Happy coding. :)

希望这可以帮助。快乐编码。:)

回答by Jonathan

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

例如: $this->load->model('Logon_model');

and:

和:

class?Logon_model?extends?CI_Model?{
...

But you are correct about the file name.

但是你对文件名是正确的。

回答by Suleiman Umar

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

我遇到了同样的问题,但我通过更改我的 application/config/routes.php 文件修复了它。

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

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

回答by user3642940

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

更改以大写开头的型号名称有效。示例:Login_model.php 而不是 login_model.php