wiredmark wiredmark - 3 years ago 158
MySQL Question

Codeigniter - Can only load model in autoload.php

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):

<?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):

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

/* Location: ./application/controllers/explore.php */


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, maybe it's just me being a little tired, but if someone could help me out i'd love that :)




After some tests I found out the error is in the

$data['changelog_row'] = $this->Changelog->get_list();


line of my code. I have no idea what the problem is. Case sensitivity is fine (also tried many combinations of lowercase/uppercase), and even if I create another function with different name (e.g. foo()) with a normal echo inside I get the SAME error but referring to that function.

Here is a screen
enter image description here




Something incredible happened: if I add the 'changelog' model in the autoload.php, it seems it can actually load it. What is going on? I used this code already in many applications without a problem.

Another test I did: if I write

public function __construct() {
parent:: __construct();
$this->load->model('changelog');
}


In the controller or just add parent:: __construct(); in the changelog function like

public function changelog() {
parent:: __construct();
$this->load->model('changelog');
$data['changelog_data'] = $this->changelog->get_list();

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


It works O_o. But why? I changed the topic title, now everything is around my inability to load my model in the controller's functions.

Answer Source

After many hours of test and messing up with everything, INCLUDING configuration files... I DID IT!

It was something totally above models and controllers, it was something about the hooks I called. In fact, I have a hook called languageloader.php written like this:

class LanguageLoader extends CI_Controller {
    public function initialize() {
        $ci =& get_instance();
        $site_lang = $ci->session->userdata('site_lang');
        if (!empty($site_lang)) {
            $ci->lang->load('text', $site_lang);
        } else {
            $ci->lang->load('text', 'english');
        }
    }
}

In my hooks file it was loaded like:

$hook['post_controller_constructor'] = array(
    'class'    => 'LanguageLoader',
    'function' => 'initialize',
    'filename' => 'languageloader.php',
    'filepath' => 'hooks',
);

Since I was using post_controller_constructor, referring to CI doc files it is

Called immediately after your controller is instantiated, but prior to any method calls happening.

I believe that doing something like my $ci =& get_instance(); I couldn't instance my damn model. I fixed it by changing the hook to

$hook['pre_controller']

I didn't think it could have been something about the hooks and that was the reasons why I didn't post it. Thanks to everyone who tried to help me out in the while. I hope this helped someone else who was in my same trouble!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download