$this->load->model() 在 CodeIgniter 中不起作用 [英] $this->load->model() not working in CodeIgniter

查看:23
本文介绍了$this->load->model() 在 CodeIgniter 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 CodeIgniter 2.1.2,这是我现在的情况.我在 C:wampwwwcr8vapplicationmodels 中有一个名为 math.php 的模型.我正在尝试从 C:wampwwwcr8vapplicationcontrollerssite.php 将它加载到我的控制器中,但我收到此错误:

I'm using CodeIgniter 2.1.2 and here's my situation right now. I have a model named math.php in C:wampwwwcr8vapplicationmodels. I am trying to load this in my controller from C:wampwwwcr8vapplicationcontrollerssite.php yet I am getting this error:

无法找到您指定的模型:math_model

Unable to locate the model you have specified: math_model

这里是math.php的内容:

 <?php
    class Math_model extends CI_Model(){
        public function add(){
            return 1+1;
        }
    }
 ?>

这是我的 site.php 的内容:

<?php
    class Site extends CI_Controller{
        public function index(){
            $this->addstuff();
        }

        public function addstuff(){
            $this->load->model("math_model");
            echo $this->math->add();
        }
    }  
?>

我在 YouTube 的教程中遵循了这一点.然而,它没有给我我想要的输出.我试过谷歌搜索并阅读其他相关问题,但这些信息还不够.

I followed this in a tutorial in YouTube. Yet, it doesn't give me my desired output. I've tried Googling about it and read other related questions with this, however those information are not enough.

我刚刚读过这个.

现在我有以下错误:

( ! ) Parse error: syntax error, unexpected '(', expecting '{' in C:wampwwwcr8vapplicationmodelsmath_model.php on line 2
Call Stack
#   Time    Memory  Function    Location
1   0.0036  148768  {main}( )   ..index.php:0
2   0.0062  185072  require_once( 'C:wampwwwcr8vsystemcoreCodeIgniter.php' )  ..index.php:202
3   0.0561  784312  call_user_func_array ( )    ..CodeIgniter.php:359
4   0.0561  784360  Site->index( )  ..CodeIgniter.php:359
5   0.0561  784376  Site->addstuff( )   ..site.php:4
6   0.0561  784504  CI_Loader->model( ) ..site.php:8

推荐答案

名称需要在所有地方都一样:

The name needs to be the same in all places:

这里:

class Math_model extends CI_Model {
   // your model
}

这里:

$this->load->model("math_model");

使用时:

$this->math_model->add();

并且:在您的文件系统中.所以将 math.php 重命名为 math_model.php 就可以了.

And: in your file system. So rename math.php to math_model.php and it will work.

这篇关于$this->load->model() 在 CodeIgniter 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆