加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Codeigniter扩展自定义模型会产生致命错误

发布时间:2020-12-13 17:45:10 所属栏目:PHP教程 来源:网络整理
导读:我已经看到了几个不同的帖子,但它们似乎都不适合我. 我有一个扩展CI_Model的类: class Users_Model extends CI_Model {public function __construct(){ parent::__construct();} 然后: class Students_Model extends Users_Model {private $_students;publ
我已经看到了几个不同的帖子,但它们似乎都不适合我.

我有一个扩展CI_Model的类:

class Users_Model extends CI_Model {

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

然后:

class Students_Model extends Users_Model {

private $_students;

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

      $this->_students = $this->get_students();
}

但是,然后我收到此错误消息:

PHP Fatal error:  Class 'Users_Model' not found in /Users/rosswilson/Localhost/thedrumrooms/dreamweaver/ci/application/models/students_model.php

我已经使用require_once将文件包含在扩展类中并且它可以工作.
这是最好的做法/正确的方法吗?

谢谢.

解决方法

CI中的标准做法是在名为MY_Model.php的应用程序/核心内创建基本模型,MY_取决于您的配置中定义的内容.

然后在你的MY_Model.php中你可以在你的模型中扩展该文件中定义的许多类,基本上CodeIgniter的加载模型在这个路径和文件中查找已定义的类.

但是如果你想使用require_once,你必须使用index.php中定义的APPPATH(应用程序路径).但是,您的自定义模型仍然需要扩展CI_Model,因为它使用CI模型的核心类,否则您的模型将无法工作.

ex require_once APPPATH.’/ models / test.php’;

增加说明:

谢谢@dean或者指出这一点.在MY_Model.php文件中,必须有一个扩展CI_Model的MY_Model类.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读