Php 消息:未定义属性:站点::$Site\u model-CodeIgniter
我正在自动加载已在自动加载配置中的库站点\ U模型,这是我得到的错误:Php 消息:未定义属性:站点::$Site\u model-CodeIgniter,php,codeigniter,Php,Codeigniter,我正在自动加载已在自动加载配置中的库站点\ U模型,这是我得到的错误: A PHP Error was encountered Severity: Notice Message: Undefined property: Site::$Site_model Filename: controllers/site.php Line Number: 16 Fatal error: Call to a member function add_record() on a non-object in
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Site::$Site_model
Filename: controllers/site.php
Line Number: 16
Fatal error: Call to a member function add_record() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/BLOCK/application/controllers/site.php on line 16
控制器:
<?php
class Site extends CI_Controller {
function index(){
$this->load->view('option_view');
}
function create(){
$data = array(
'subject' => $this->input->post('subject'),
'body' => $this->input->post('body')
);
$this->Site_model->add_record($data);
$this->index();
}
}
?>
型号:
<?php
class Site_model extends CI_Model {
function get_records()
{
$query = $this->db->get('items');
return $query->result();
}
function add_record()
{
$this->db->insert('items', $data);
$return;
}
function update_record()
{
$this->db->where('id', 1);
$this->db->update('items', $data);
}
function delete_record()
{
$this->db->where('id', $this->url->segment(3));
$this->db->delete('items');
}
}
?>
以及以下观点:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>option_view</title>
<style type="text/css" media="screen">
label {display:block;}
</style>
</head>
<body>
<h2>Create</h2>
<?php echo form_open('site/create'); ?>
<p>
</label for="subject">Subject</label>
<input type="text" name="subject" id="subject">
</p>
<p>
</label for="body">Body</label>
<input type="text" name="body" id="body">
</p>
<p>
<input type="submit" value="Submit">
</p>
<?php echo form_close();?>
</body>
</html>
选项u视图
标签{显示:块;}
创造
主题
身体
你们怎么看
非常感谢$this->load->model('Site_model');#站点\模型->添加\记录($data);
$this->load->model('Site_model'); # <- add this
$this->Site_model->add_record($data);
您的代码很好……但您只忽略了一件简单的事情。您忽略了在控制器中提供以下内容“\uu Construct()”
就是这样。一切都定义得很好。添加构造函数。然后调用模型会有所帮助
<?php
class some_controller extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('site_model');
}
function index()
{
$this->load->view('your_view');
}
function create()
{
$data = array(
'test1' => $this->input->post('test1'),
'test2' => $this->input->post('test2')
);
$this->site_model->add($data);
$this->index();
}
在config autoload.php
上添加“站点模型”,如下所示:
$autoload['model'] = array('Site_model');
可能重复感谢我已经在配置中自动加载模型,但缺少大写字母S!!
$autoload['model'] = array('Site_model');