database Codeigniter 未定义属性: xxxx_model::$db 仅来自模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6415488/
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
Codeigniter Undefined property: xxxx_model::$db only from Model
提问by Stephane Grenier
First the Model class:
首先是模型类:
class Xxxx_model extends Model
{
function XxxxModel()
{
parent::Model();
$this->load->database();
}
function isInDatabase()
{
// Please ignore the sql query, it's just to show some random sql code with results
11. $result = $this->db->query('SELECT * FROM someTable WHERE ...');
$numberOfRows = $result->num_rows();
...
return $test;
}
}
Now the controller:
现在控制器:
function someLogic()
{
$this->load->model('xxxx_Model', 'xxxxModel'); // not necessary to specify
$this->xxxxModel->isInDatabase();
}
When I run this I get the error:
当我运行它时,我收到错误:
Severity: Notice --> Undefined property: Xxxx_model::$db .../xxxx_model.php line 11
I have no idea why this is. If I put the db code in the controller it seems to work, it's only with this setup in the model that it fails. I can't for the life of me figure out where the code is astray...
我不知道这是为什么。如果我将 db 代码放在控制器中,它似乎可以工作,只有模型中的此设置才会失败。我一生都无法弄清楚代码在哪里误入歧途......
采纳答案by Wesley Murch
To add to atno's answer:
添加到 atno 的答案:
class Xxxx_model extends Model
{
function XxxxModel() //<--- does not match model name Xxxx_model
{
parent::Model();
$this->load->database();
}
Basically, you are not constructing the class orthe parent class Model. If you are on PHP5, you may use __construct(), otherwise you must match the class name exactly, regardless of what alias you load it with in your controller. Example:
基本上,您不是在构建类或父类Model。如果您使用的是 PHP5,则可以使用__construct(),否则您必须完全匹配类名,无论您在控制器中使用什么别名加载它。例子:
class Xxxx_model extends Model
{
function __construct()
{
parent::__construct(); // construct the Model class
}
}
I may be mistaken (haven't used 1.x in a while), but if you construct the Model class, there's no need to load the database if you are using the default connection settings in config/database.php, it should already be loaded for you.
我可能弄错了(有一段时间没有使用 1.x),但是如果您构建 Model 类,如果您使用的是 中的默认连接设置config/database.php,则不需要加载数据库,它应该已经为您加载了。
回答by uwublogs
You have to load the db library first. In autoload.phpadd below code,
您必须先加载 db 库。在autoload.php添加下面的代码,
$autoload[‘libraries'] = array(‘database');
回答by Tonci14
add library 'datatabase' to autoload.
将库“数据库”添加到自动加载。
/application/config/autoload.php
/application/config/autoload.php
$autoload['libraries'] = array(
'database'
);
$autoload['libraries'] = array(
'database'
);
Propably you're started new project, like me ;-)
可能你和我一样开始了新项目;-)
回答by afarazit
If function XxxxModel()isn't your constructor, you're not loading the database by calling $this->xxxxModel->isInDatabase();
如果function XxxxModel()不是您的构造函数,则不会通过调用加载数据库$this->xxxxModel->isInDatabase();
Try autoloading the database library from within autoload.php, or create a proper constructor in your model.
尝试从 内部自动加载数据库库autoload.php,或在您的模型中创建适当的构造函数。

