php codeigniter:调用未定义的方法 CI_Loader::model()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21651961/
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-25 04:19:28  来源:igfitidea点击:

php codeigniter: Call to undefined method CI_Loader::model()

phpcodeignitermodel

提问by Lewis

I wanted to make a webapplication using codeigniter but it is a while since I've used it and I'm getting an error whenever I try to load a model in my controller. I'm probably doing something stupid wrong but I can't figure out what it is. So please help me out if u can.

我想使用 codeigniter 制作一个 web 应用程序,但自从我使用它以来已经有一段时间了,每当我尝试在我的控制器中加载模型时,我都会收到错误消息。我可能在做一些愚蠢的错误,但我无法弄清楚它是什么。如果可以,请帮助我。

This is the error I get:

这是我得到的错误:

error

错误

Here is the code of my model:

这是我的模型的代码:

<?php 

class post_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function getAllPosts() {
        $this->db->order_by('date', 'desc');
        $query = $this->db->get('post');
        return $query->result();
    }
}
?>

Here is the code of my controller where I load the model:

这是我加载模型的控制器代码:

<?php 

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Post extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->model('post_model');
    }

    public function index() {
        $data['title'] = 'Berichten';
        $data['posts'] = $this->post_model->getAllPosts();

        $this->template->load('posts', $data);
    }
}

Autoload:

自动加载:

$autoload['helper'] = array('url', 'form', 'date');
$autoload['model'] = array();

Solved:I couldn't solve it but I made a new project and copy-pasted my code and now it works just fine, so no idea what was wrong.

已解决:我无法解决它,但我创建了一个新项目并复制粘贴了我的代码,现在它可以正常工作了,所以不知道出了什么问题。

回答by jtheman

All classes should be uppercase at the first letter

所有类的第一个字母都应该是大写的

class Post_model extends CI_Model {

This throws the error when loading the model at line 11

这会在第 11 行加载模型时引发错误

回答by xiaodx

class post_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();//加上这句
    }

    function getAllPosts() {
        $this->db->order_by('date', 'desc');
        $query = $this->db->get('post');
        return $query->result();
    }
}

回答by Abubakar

I think your model file extension .html... change your file extention .php... correct: model_file.phpincorrect: model_file.html

我认为您的模型文件扩展名.html...更改您的文件扩展名.php...正确:model_file.php不正确:model_file.html

Check your file format.

检查您的文件格式。

回答by amey kajarekar

Class name must be start with the capital letter and the file name must have the same name of that class with the extension .php Also remove the closing php tag from the model.The codeigniter class files doesn't need any close tag.It will closes automatically by the framework.

类名必须以大写字母开头,文件名必须与该类的名称相同,扩展名为 .php 还要从模型中删除关闭 php 标签。codeigniter 类文件不需要任何关闭标签。它会由框架自动关闭。