简体   繁体   English

尝试加载模型时出现错误

[英]Getting an error when trying to load a model

I'm loading a model for my CodeIgniter based website, but it keeps giving an error: 我正在为基于CodeIgniter的网站加载模型,但该模型始终显示错误:

"Fatal error: Call to a member function on a non-object in /nfs/c02/h05/mnt/30796/domains/planetchustar.com/html/arguit/system/application/controllers/home.php on line 8"

Here's the part of code its referencing: 这是其引用的代码部分:

function index()
{
        $this->load->model('posts');    //error here
        $result = $this->Posts->get_all_topics();
}

The model is called "Posts" and its filename is "posts.php". 该模型称为“ Posts”,其文件名为“ posts.php”。

EDIT: I found out one of my problems, which was that I wasn't loading to database before I tried to use its functions, so I fixed that, but now its saying: 编辑:我发现了我的问题之一,那就是我在尝试使用它的功能之前没有加载到数据库,所以我解决了这个问题,但是现在它的说法是:

A Database Error Occurred
Unable to connect to your database server using the provided settings.

But I'm certain the connection info I saved in the database.php file is accurate (got from the phpmyadin website). 但是我确定我保存在database.php文件中的连接信息是正确的(从phpmyadin网站获取)。

Is 'posts' in a subdirectory? “帖子”在子目录中吗? If so then you need to reference the subdirectory during load. 如果是这样,则需要在加载期间引用该子目录。

If it's not that then here is a post which may help. 如果不是那样,那么这里可能有帮助。

I agree with @arthur. 我同意@arthur。 Make sure you haven't tried to embed a controller within another controller. 确保您没有尝试将一个控制器嵌入另一个控制器中。

It's a case sensitivity error.... 这是区分大小写的错误。

use it like so: 像这样使用它:

function index()
{
        $this->load->model('posts');    //error here
        $result = $this->posts->get_all_topics();//<-- Notice "posts" and not "Posts"
}

This means that $this->load is not an object reference (most likely null). 这意味着$ this-> load不是对象引用(很可能为null)。 Why maybe easy to determine once you know this. 为什么一旦知道就容易确定。

The codeigniter user guide is a little difficult to understand in this case. 在这种情况下,codeigniter用户指南有点难以理解。 ONLY the file name should be lower case, the 'load' and the call ($this->Posts->etc()) should match the declaration in the model file. 仅文件名应小写,“ load”和调用($ this-> Posts-> etc())应与模型文件中的声明匹配。

It's is definitely a case sensitivity issue. 这绝对是区分大小写的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM