Welcome Guest, Not a member yet? Register   Sign In
Codeigniter - Can only load model in autload.php
#1

[eluser]Unknown[/eluser]
Hi everyone,

I'm going crazy because I have been using Codeigniter for ages now and I cannot seem to load a model in my view.

This is what I have done. Model code (models/changelog.php):

Code:
?php

class Changelog extends CI_Model {
    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    function get_list() {
        $query = $this->db->get('changelog');
        return $query->result();        
    }
}

/* Location: ./application/models/changelog.php */

This is my controller (controllers/explore.php):

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Explore extends CI_Controller {
    public function index() {
        $this->load->view('include/header');
        $this->load->view('home');
        $this->load->view('include/footer');
    }

    public function changelog() {
        $this->load->model('changelog');
        $data['changelog_row'] = $this->changelog->get_list();

        $this->load->view('include/header');
        $this->load->view('explore/changelog', $data);
        $this->load->view('include/footer');
    }
}

I get a Codeigniter notice telling me Message: Undefined property: Explore::$Changelog and a PHP error, Fatal error: Call to a member function get_list() on a non-object.

Here is what I did

Tried to autoload the database instead of only loading it in that model
Tried changing the Changelog call in the contorller to lowercase
Checked the connection to the database
Enabled the log file, which doesn't tells me anything new


Everything works correctly.

After a while I found out something incredible happened: if I add the 'changelog' model in the autoload.php, it seems it can actually load it. What is going on?

Another test I did: if I write

Code:
public function __construct() {
        parent:: __construct();
        $this->load->model('changelog');
}
In the controller it works. But why? Why can't I load the model in my function in this case? I cannot believe it, hope someone out there could help me out and that this isnt' a CI 2.2.0 bug. I did this thousand times and without problems..


Messages In This Thread
Codeigniter - Can only load model in autload.php - by El Forum - 10-26-2014, 02:18 PM
Codeigniter - Can only load model in autload.php - by El Forum - 10-26-2014, 04:14 PM



Theme © iAndrew 2016 - Forum software by © MyBB