Codeigniter - 调用非对象上的成员函数
有人可以向我解释一下为什么我收到以下错误吗?我之前已经收到过很多次错误,但没有找到原因:
Fatal error: Call to a member function category_exists() on a non-object in C:\wamp\www\application\controllers\news.php on line 50
控制器(news.php):
...
public function category($category,$id,$title){
if($this->news_model->category_exists($category) == true){
echo 'true';
}
else {
echo 'false';
}
}
...
The model:
function category_exists($category)
{
$this->db->where('news_category',$category);
$query = $this->db->get('news_category');
if ($query->num_rows() > 0){
return true;
}
else{
return false;
}
}
如果您能解释一下错误是什么意味着以及为什么它是一个非对象,或者如何将它变成一个很棒的对象......
编辑:模型自动加载
Could somebody please explain to me why I am receiving the below error, I have received it many times before and have not found out why:
Fatal error: Call to a member function category_exists() on a non-object in C:\wamp\www\application\controllers\news.php on line 50
The controller (news.php):
...
public function category($category,$id,$title){
if($this->news_model->category_exists($category) == true){
echo 'true';
}
else {
echo 'false';
}
}
...
The model:
function category_exists($category)
{
$this->db->where('news_category',$category);
$query = $this->db->get('news_category');
if ($query->num_rows() > 0){
return true;
}
else{
return false;
}
}
If you could explain what the error means and why it is a non-object or how to turn it into an object that would be great....
EDIT: MODEL IS AUTOLOADED
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
复制回答:
我可能不对,但是型号名称不区分大小写吗?因此,如果您的模型是类 News_model,则应通过 $this->News_model->category_exists 调用它。 (尽管您省略了声明,所以这只是一个猜测)
Copied to answer:
I may be off, but aren't model names case sensitive? So if your model is class News_model it should be called by $this->News_model->category_exists. (though you have omitted the declaration, so this is only a guess)
你有
config/autoload.php
吗?如果是,则将其加载到
config/autoload.php
文件中(在底部)$autoload['model'] = array();
如果没有,则加载它:
模型文件名必须与模型类名相同。
也用用户指南检查构造函数。
Do you have it
config/autoload.php
?If yes then load it in the
config/autoload.php
file (In the bottom)$autoload['model'] = array();
If not,load it:
The model file name must be the same as the model class name.
Check constructor with user guide too.