Welcome Guest, Not a member yet? Register   Sign In
Issues loading models

Hey everyone, first time caller!

I am working though the Wrox Professional Codeigniter book and I am running into a small snag. I worked through chapter 3 and got everything configured, the models, the controller and the views setup. However when I try and and run the app I get the following message

Fatal error: Call to undefined method MCats::getCategoriesNav() in F:\xampp\htdocs\CI\system\application\controllers\welcome.php on line 13\

here is what the code looks like for the index function on the controller which is welcome.php

lass Welcome extends Controller {

    function Welcome(){
    function index(){
        $data['title'] = "Welcome to Claudia's Kids";
        $data['navlist'] = $this->MCats->getCategoriesNav();


here is a snippet of my mcats.php file which where the model in question is at

function getCategoriesNav() {
    $data = array();
    $Q = $this->db->get('categories');
    if ($Q->num_rows() > 0){
        foreach ($Q->result_array() as $row) {
            $data[$row['id']] = $row['name'];

    return $data;

here is what the line in my autoload.php that deals with loading the Models

$autoload['model'] = array('MProducts', 'MCats');

I am kinda scratching my head on this one, I turned up logging and didn't see anything worth while that could help. It kinda looks like that maybe the model isn't getting loaded properly which would through the undefined method error. I just can't seem to find whats going on with it and sometimes it takes a fresh set of eyes to spot the problem.

Thanks for the help!

Please can you post the top line of your model, and the constructor for your model? Sounds like you might not be initialising it properly.

Also, the call to the model should have the model name in lowercase.
$data['navlist'] = $this->mcats->getCategoriesNav();

If you want to use MCats as your model name, you can low your model like this:
$this->load->model('mcats', 'MCats');
then you can use it like you have been. I'm not sure how pass a second parameter via the auto load file, however.

Oh, and welcome to the CodeIgniter community! (Sorry, I seem to be easily distracted. Oh look, a shiny penny!)

I found out what the problem was. I went to check the model file again and noticed (and this is really sad) that I didn't wrap any of the functions into the class like I should have. Sometimes its best to walk away and come back to it in a few hours with fresh eyes!

Theme © iAndrew 2016 - Forum software by © MyBB