在 CodeIgniter 控制器中找不到模型

Can't find model in CodeIgniter controller

我在第 8 行加载了 Get_notes 模型,但是当我想在第 23 行使用 Get_notes 模型加载 add_notes 方法时,出现错误并显示 Undefined property: Notes::$Get_notes 在第 23 行! 第 23 行有问题,但我不知道是什么 that.Please 帮助我。 谢谢

<?php

class Notes extends CI_Controller
{

    public function index()
    {
        $this->load->model('Get_notes');
        $data['notes'] = $this->Get_notes->get_mm();
        $this->load->view('show', $data);
    }

    public function insert()
    {
        $title = 'Something';
        $text = 'Something else';

        $data = [
            'title' => $title,
            'text' => $text
        ];

        $this->Get_notes->add_notes($data);
    }
}

您的 get_notes 模型仅在索引函数中加载。您不能在索引函数中加载模型并在任何其他函数中使用它而无需再次加载它。 我在想您正在尝试一次加载模型并在整个控制器中使用它。为此,您必须改为在 __construct 方法中加载它。您的代码应与此类似:

<?php

class Notes extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Get_notes');
    }

    public function index()
    {
        $data['notes'] = $this->Get_notes->get_mm();
        $this->load->view('show', $data);
    }

    public function insert()
    {
        $title = 'Something';
        $text = 'Something else';

        $data = [
            'title' => $title,
            'text' => $text
        ];

        $this->Get_notes->add_notes($data);
    }
}

把$this->load->model('Get_notes');在 insert() 函数中。

如果你想全局使用它,把它放在构造函数中。

按照您编写的方式,您的 Get_notes 模型仅在您的 index() 函数中可用。


要使您的 Get_notes 模型可用于单个控制器中的所有函数,请将其加载到 Controller's constructor function...

class Notes extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('get_notes'); // available to all functions within Notes 
    }
    ....

要使您的 Get_notes 模型可全局用于任何 CI 控制器中的所有函数,请将其放入位于 autoload.php 文件中的 $autoload['model'] 数组中19=]...

$autoload['model'] = array('get_notes'); // available to all functions in your CI application

→注意不管后面怎么引用,都应该全部小写

$this->load->model('get_notes');
$this->get_notes->add_notes($data);

参见:

If you intend to use a constructor in any of your Controllers, you MUST place the following line of code in it:

parent::__construct(); // Notice there is no dollar sign

Where Model_name is the name of your class. Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.

Your models will typically be loaded and called from within your controller methods. To load a model you will use the following method:

$this->load->model('model_name');

Once loaded, you will access your model methods using an object with the same name as your class:

$this->model_name->method();

To autoload resources, open the application/config/autoload.php file and add the item you want loaded to the autoload array. You’ll find instructions in that file corresponding to each type of item.

codeigniter 模型调用区分大小写,因此要获得您应该使用的模型

$this->get_notes->a_function_inside_the_model ();

另请注意,模型文件夹中的名称应始终以大写字母开头。

每当我们在像 wamp 服务器这样的本地主机服务器上 运行 codeigniter 时,这些问题不存在,但在实时服务器上它们会。

希望这对您有所帮助