【问题标题】:Codeigniter: My controller is not able to access the modelCodeigniter:我的控制器无法访问模型
【发布时间】:2011-06-27 13:34:13
【问题描述】:

//我的控制器部分

<?php
class Myadmin extends CI_Controller 
{
            public function _construct()    
            {
             parent::_construct();
             $this->load->library('form_validation');
             $this->load->helper('form');
             $this->load->model('adder','',TRUE);
            }

            public function index() 
            {
                    echo " i am about to call the model";
                    $this->adder->insert_user();        
            }
}
?>    
**//My model section**

<?php
class Adder extends CI_Model    {
        function_construct()    {
            parent::_construct();
        }

        public function insert_user()   
        {
              echo " Hi ,the model is accessed";    
        }
}
?>

【问题讨论】:

  • 这些实际上不在同一个文件中,是吗?

标签: codeigniter model controller


【解决方案1】:

是因为“function_construct()”吗? 它没有空间,你应该使用两个_

函数_构造(){ 父::_construct(); } 控制器中相同

【讨论】:

    【解决方案2】:

    问题在于您在控制器中加载模型的方式。

    在当前版本的 CodeIgniter 中,你应该这样做:

    //loading the model
    $this->load->model('adder', 'fubar');
    
    //accessing it's functions
    $this->fubar->function();
    

    更多信息请见this

    编辑: 您已经定义了一个必须是 __construct() 的 _construct() 函数。 此外,您应该将parent::_construct(); 修复为parent::__construct()

    【讨论】:

    • 正如 WordsWorth 所提到的,您的代码的构造函数也存在问题。
    猜你喜欢
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    相关资源
    最近更新 更多