php - Codeigniter - 只能在 autoload.php 中加载模型

标签 php mysql database codeigniter model

我快要疯了,因为我已经使用 Codeigniter 多年了,但我似乎无法在我的 View 中加载模型。

这就是我所做的。模型代码(models/changelog.php):

<?php

class Changelog extends CI_Model {
    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    function get_list() {
        $query = $this->db->get('changelog');
        return $query->result();        
    }
}

/* Location: ./application/models/changelog.php */

这是我的 Controller (controllers/explore.php):

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

class Explore extends CI_Controller {
    public function index() {
        $this->load->view('include/header');
        $this->load->view('home');
        $this->load->view('include/footer');
    }

    public function changelog() {
        $this->load->model('changelog');
        $data['changelog_row'] = $this->changelog->get_list();

        $this->load->view('include/header');
        $this->load->view('explore/changelog', $data);
        $this->load->view('include/footer');
    }
}

/* Location: ./application/controllers/explore.php */

我收到一条 Codeigniter 通知,告诉我消息:未定义的属性:Explore::$Changelog 和一个 PHP 错误, fatal error :调用非成员函数 get_list()对象

这是我做的

  • 尝试自动加载数据库而不是仅在该模型中加载它
  • 尝试将 Controller 中的 Changelog 调用更改为小写
  • 检查数据库连接
  • 启用日志文件,但没有告诉我任何新信息

一切正常,也许只是我有点累,但如果有人能帮助我,我会很高兴:)


经过一些测试我发现错误在

$data['changelog_row'] = $this->Changelog->get_list();

我的代码行。我不知道问题出在哪里。大小写敏感度很好(也尝试了很多小写/大写的组合),即使我创建了另一个具有不同名称的函数(例如 foo())并且内部有正常回显,我也会得到相同的错误但引用该函数。

这是一个屏幕 enter image description here


不可思议的事情发生了:如果我在 autoload.php 中添加 'changelog' 模型,它似乎真的可以加载它。到底是怎么回事?我已经在许多应用程序中毫无问题地使用了这段代码。

我做的另一个测试:如果我写

public function __construct() {
        parent:: __construct();
        $this->load->model('changelog');
}

在 Controller 中或者只是添加 parent::__construct(); 在 changelog 函数中,例如

public function changelog() {
        parent:: __construct();
        $this->load->model('changelog');
        $data['changelog_data'] = $this->changelog->get_list();

        $this->load->view('include/header');
        $this->load->view('explore/changelog', $data);
        $this->load->view('include/footer');
    }

它有效 O_o。但为什么?我更改了主题标题,现在一切都围绕着我无法在 Controller 的函数中加载我的模型。

最佳答案

CI 对此表现得有点奇怪。 在 unix 系统(文件名区分大小写)中,您必须使用区分大小写的名称加载模型。 但是在 Controller 中你应该用小写来解决它,即

$this->load->model('changeLog');
$this->changelog->getList();

关于php - Codeigniter - 只能在 autoload.php 中加载模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26573466/

相关文章:

python - 使用 MySQL 管理 Web 应用程序的用户身份验证

java - 如何访问 java Derby 数据库?我确实需要一些有用的建议

php - 从实例配置文件元数据服务器检索凭据时出错

javascript - 从动态表mysql php获取值

php - 如何查看MySql当前查询包大小?

mysql - 查询以检索字段中包含重复值的行的 ID

database - 两个线程同时添加新行 - 如何防止?

mysql - 我想在新计算机上使用来自非工作服务器备份的 mysql 数据库

php - 更改 Wordpress 仪表板 Logo (多站点)

javascript - 如何使用AJAX、PHP和MySQL实时发出通知?