Codeigniter扩展自定义模型会产生严重错误 [英] Codeigniter Extend custom model produces FATAL ERROR
问题描述
我有一个类可以扩展CI_Model:
我已经看过几个不同的帖子, p> class Users_Model extends CI_Model {
public function __construct(){
parent :: __ construct );
}
,然后:
class Students_Model extends Users_Model {
你可以在该文件中定义许多类,你可以在你的模型中扩展,基本上CodeIgniter的负载模型在这个路径和文件中查找已定义的类。
private $ _students;
public function __construct(){
parent :: __ construct();
$ this-> _students = $ this-> get_students();然而,我会收到此错误消息:
PHP致命错误:未在/Users/rosswilson/Localhost/thedrumrooms/dreamweaver/ci/application/models/students_model.php中找到类'Users_Model
我使用
require_once
扩展类和它的工作原理。
这是最佳做法/正确的方法吗?
谢谢。
方案CI的标准做法是在
application / core
中创建一个名为MY_Model.php
,MY _
取决于您配置中定义的内容。
> MY_Model.php
但是如果你想使用
require_once
,你必须使用APPPATH
应用程序路径)定义在index.php
。但是您的自定义模型仍然必须扩展CI_Model
,因为它使用CI模型的核心类或您的模型将无法工作。
ex
require_once APPPATH。'/ models / test.php';
:
谢谢@dean或指出。在
MY_Model.php
文件中,必须 是MY_Model类
扩展CI_Model
。I've seen a few different posts on this, but none of them seem to be working for me.
I have one class that extends CI_Model:
class Users_Model extends CI_Model { public function __construct(){ parent::__construct(); }
and then:
class Students_Model extends Users_Model { private $_students; public function __construct(){ parent::__construct(); $this->_students = $this->get_students(); }
However I then receive this error message:
PHP Fatal error: Class 'Users_Model' not found in /Users/rosswilson/Localhost/thedrumrooms/dreamweaver/ci/application/models/students_model.php
I've used
require_once
to include the file in the extended class and it works. Is this the best practice/correct way to do this?Thanks.
解决方案The standard practice in CI is to create a Base Model inside
application/core
namedMY_Model.php
,MY_
depends on what is defined in your config.Then inside your
MY_Model.php
you can have many classes defined in that file that you can extend in your Model, basically CodeIgniter's load model looks for defined classes on this path and file.But if you want to use
require_once
you have to use theAPPPATH
(application path) as defined onindex.php
. But your custom model would still have to extendCI_Model
as it uses the core class for CI Model or your model will not work.ex
require_once APPPATH.'/models/test.php';
ADDED NOTES:
thank you @dean Or for pointing that out. Inside the
MY_Model.php
file there must be aMY_Model class
that Extends theCI_Model
.这篇关于Codeigniter扩展自定义模型会产生严重错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!