控制器 - CodeIgniter中的模型和“未定义的属性”

时间:2016-02-17 18:09:55

标签: php codeigniter

<?php
class Success_model extends CI_Model
{
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    //insert into user table
    function get_all()
    {
        $query = $this->db->get('session'); // = select * from session
        return $query->result();
    }

}
<?php 
class Success extends CI_Controller
{

     public function __construct()
     {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->model('success_model');
    }

     public function index()
     {
        $data= $this->Success_model->get_all();
        $this->load->view('success_view', $data);     
      }
}
?>

你可以看到这很好,但我有错误:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Success::$Success_model

Filename: controllers/success.php

Line Number: 15

Backtrace:

File: C:\xampp\htdocs\session\application\controllers\success.php
Line: 15
Function: _error_handler

File: C:\xampp\htdocs\session\index.php
Line: 292
Function: require_once


Fatal error: Call to a member function get_all() on null in C:\xampp\htdocs\session\application\controllers\success.php on line 15
A PHP Error was encountered

Severity: Error

Message: Call to a member function get_all() on null

Filename: controllers/success.php

Line Number: 15

Backtrace:

我正在寻找,但我不知道什么是坏事。好的目录是文件,其他类正在工作,但事实并非如此。

请帮忙。我只是累了,因为经过2个小时搜索错误......

1 个答案:

答案 0 :(得分:1)

对于CI v3,afaik

我想我明白了:

$data= $this->Success_model->get_all();

应该是:

$data = $this->success_model->get_all(); // Notice the lowercase success_model

另一个解决方案是改变

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

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

因为那样你可以使用

$data= $this->Success_model->get_all();