Search code examples
phpcodeigniter

Controller - Model in CodeIgniter and "Undefined property"


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

As You can see this is good, but I have error:

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:

I'm looking, and looking, but I dont know what is bad. The good directories are files, other classes are working but this not.

Please help. I'm just tired, because after 2 hours searching mistakes...


Solution

  • For CI v3, afaik


    I think I see it:

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

    Should be:

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

    PHP variables are case-sensitive where as functions and class names are not.


    One more solution would be to change

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

    into

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

    because then you can use

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