在CodeIgniter中扩展模型 [英] Extends Model in CodeIgniter
问题描述
好吧,我是CodeIgniter Framework的新手,我正在尝试构建通用的Model类。参见:
Well, I'm newbie in CodeIgniter Framework and i'm trying building a generic Model Class. See:
class Basic_Model extends CI_MODEL {
function __construct() {
// Call the Model constructor
parent::__construct();
}
}
我想扩展所有基于在Basic_Model上,像这样:
I want to extends all Models based on Basic_Model, like this:
class Pagina_Model extends Basic_Model {
function __construct() {
// Call the Model constructor
parent::__construct();
}
}
问题是当我尝试从控制器调用 pagina_model时出现以下错误:
The problem is when i try to call "pagina_model" from a Controller a got the following error:
Fatal error: Class 'Basic_Model' not found in /var/www/myproject/application/models/pagina_model.php on line 12
如果我使用 basic_model控制器一切正常。
If i use "basic_model" in controller everything works fine.
编辑1:
我在中创建了一个名为MY_Basic_Model.php的文件/ application / core,并将类名称更改为 MY_Basic_Model。但是我得到了错误:
I created a file named MY_Basic_Model.php in "/application/core" and changed the class name to "MY_Basic_Model". But i got the error:
Fatal error: Class 'MY_Basic_Model' not found in /var/www/myproject/application/models/pagina_model.php on line 12
推荐答案
为此创建核心系统类(也称为方法覆盖)。
创建文件 application / core /
目录中的 MY_Model.php
>扩展基本的 CI_Model
类:
Create a file MY_Model.php
in application/core/
directory which will extend the base CI_Model
class:
<?php
class MY_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
现在您可以在模型中进行扩展 (../ applicaton / models /):
Now you can extend this in your models (../applicaton/models/):
<?php
class Pagina_Model extends MY_Model {
function __construct()
{
parent::__construct();
}
}
这里没有什么要注意的:
1)类声明必须扩展父类。
2)您新的类名称和文件名必须以 MY _ 为前缀(此项是可配置的。)
Few things to note here:
1) The class declaration must extend the parent class.
2) Your new class name and filename must be prefixed with MY_ (this item is configurable).
如何配置:
要设置自己的子菜单,类前缀,打开您的application / config / config.php文件并查找以下项目:
How to configure:
To set your own sub-class prefix, open your application/config/config.php file and look for this item:
$config['subclass_prefix'] = 'MY_';
文档:
https://ellislab.com/codeigniter/user-guide/general/core_classes.html
这篇关于在CodeIgniter中扩展模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!