php 消息:未定义的属性:Site::$Site_model - CodeIgniter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11747665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:59:22  来源:igfitidea点击:

Message: Undefined property: Site::$Site_model - CodeIgniter

phpcodeigniter

提问by user1250526

I am auto loading the library site_model already in autoload config, and this is the error I am getting:

我正在自动加载已经在自动加载配置中的库 site_model,这是我得到的错误:

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

controller:

控制器:

<?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();

}

}


?>

model:

模型:

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

}

}





?>

and the view:

和观点:

<!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>

What do you guys reckon?

大家怎么看?

Much appreciated

非常感激

回答by Alexander Larikov

$this->load->model('Site_model'); # <- add this
$this->Site_model->add_record($data);

回答by Gautam3164

Your code is nice....but you forgotted only simple thing.You forgotted to give the below in your Controller "__Construct()"

你的代码很好......但你只忘记了简单的事情。你忘了在你的控制器“__Construct()”中给出以下内容

$this->load->model('your modelname');

That's it.everythng is well defind.

就是这样。一切都很好。

回答by NIMISHAN

Add a constructor. Then it will help to call the model.

添加构造函数。然后它将有助于调用模型。

<?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();
    }

}

}