CodeIgniter $ this-> load-> model()不工作 [英] CodeIgniter $this->load->model() not working

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

问题描述

Im using codeigniter 2.1.2 and here's my situation now now。我有一个名为math.php的模型在C:\wamp\www\cr8v\application\models。我试图从我的控制器加载这个从C:\wamp\www\cr8v\application\controllers\site.php但是得到这个错误:

Im using codeigniter 2.1.2 and here's my situation right now. I have a model named math.php in C:\wamp\www\cr8v\application\models. I am trying to load this in my controller from C:\wamp\www\cr8v\application\controllers\site.php yet im getting this error:

Unable to locate the model you have specified: math_model

这里是math.php的内容

Here's the content of math.php

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

这里是我的site.php的内容

And here's the content of my 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 doesnt give me my desired output. Ive tried googling about it and read other related questions with this, however those information are not enough. Please help.

已编辑
i已读过这段时间

EDITED i have read this just awhile ago

[http://stackoverflow.com/questions/8074368/codeigniter-php-model-access-unable-to-locate-the-model-you-have-specified] [1]

[http://stackoverflow.com/questions/8074368/codeigniter-php-model-access-unable-to-locate-the-model-you-have-specified][1]

现在我有这个错误

( ! ) Parse error: syntax error, unexpected '(', expecting '{' in C:\wamp\www\cr8v\application\models\math_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:\wamp\www\cr8v\system\core\CodeIgniter.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.

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

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