• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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..

#2
[eluser]Unknown[/eluser]
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:

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

Code:
$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

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

Code:
$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!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.