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

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

Code:
lass Welcome extends Controller {

    function Welcome(){
        parent::Controller();
        
    }
    
    function index(){
        
        $data['title'] = "Welcome to Claudia's Kids";
        $data['navlist'] = $this->MCats->getCategoriesNav();
        $this->load->vars($data);
        $this->load-view('template');
    }

...


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

Code:
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'];
            }
    }

    $Q->free_result();
    return $data;
}

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

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

[eluser]TheFuzzy0ne[/eluser]
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.
#3

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

If you want to use MCats as your model name, you can low your model like this:
Code:
$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!)
#4

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