简体   繁体   English

我的模型未在codeigniter的控制器中加载

[英]my model does not load in the controller in codeigniter

This is my controller: 这是我的控制器:

defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('User_model');
$this->load->helper(array('form','text','url','array'));
$this->load->library(array('Form_validation','email','session'));
}

public function index()
{

$this->model->insert_item();
$this->load->view('welcome_message');
}
}

This is my model: 这是我的模型:

defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model {
public function insert_item()
{
echo "hi";
}
}

when i call the model function but it does not load it show error Fatal error: Call to a member function insert_item() on a non-object 当我调用模型函数但未加载时显示错误致命错误:在非对象上调用成员函数insert_item()

You need to call $this->user_model, that's the CI pattern to objects mounted: 您需要调用$ this-> user_model, 这是挂载对象的CI模式

defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('user_model');
$this->load->helper(array('form','text','url','array'));
$this->load->library(array('Form_validation','email','session'));
}

public function index()
{

$this->user_model->insert_item();
$this->load->view('welcome_message');
}
}

Loading a Model 加载模型

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');

If your model is located in a sub-directory, include the relative path from your models directory. 如果您的模型位于子目录中,请包括来自模型目录的相对路径。 For example, if you have a model located at application/models/blog/Queries.php you'll load it using: 例如,如果您有一个模型位于application / models / blog / Queries.php中,则可以使用以下命令加载它:

$this->load->model('blog/queries');

Once loaded, you will access your model methods using an object with the same name as your class: 加载后,您将使用与您的类同名的对象访问模型方法:

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

$this->model_name->method();

If you would like your model assigned to a different object name you can specify it via the second parameter of the loading method: 如果您希望将模型分配给其他对象名称,则可以通过loading方法的第二个参数进行指定:

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

$this->foobar->method();

TIP Unrelated to the question: Avoid call all the basic helpers on construct, put It on autoload. 提示与问题无关:避免在构造函数上调用所有基本辅助函数,而将其置于自动加载状态。

open your autoload.php file in 在打开您的autoload.php文件

application/config/autoload.php add application / config / autoload.php添加

$autoload['model'] = array('user_model');

You are using incorrect syntax. 您使用的语法不正确。 Here is the documentation: https://www.codeigniter.com/userguide3/general/models.html 这里是文档: https : //www.codeigniter.com/userguide3/general/models.html

This is the correct way to call model functions: 这是调用模型函数的正确方法:

$this->User_model->insert_item();

When you use this $this->load->model('User_model'); 当您使用此$this->load->model('User_model'); statement to load the model, CodeIgniter automatically create member variable with that name. 语句以加载模型,CodeIgniter会自动使用该名称创建成员变量。 So, you can use that member variable to call model functions. 因此,您可以使用该成员变量来调用模型函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM