1

I created a new model class as my_model.php inside /models folder, and a function inside it to load all elements:

function get_all(){
    $query = $this->db->get('test'); //test is my table 
   return $query->result();
}

In the controller, I instantiated the class and called the method;

$this->load->model('my_model');
$res = $this->my_model->get_all();

But, this throws me error saying:

Fatal error: Call to a member function get() on a non-object in /var/www/testapp/application/models/my_model.php on line 7

This line 7 points to the portion of the code where I have used $this->db. I tried to see the value of $db but I think it is magic accessor __get and __set, so I could not see the value of this property before calling that method.

I tried googling for several other cases but none of them match my scenarios and rather none of them could solve my problem.

3 Answers 3

6

You have to load the Database first

$this->load->database();

So, all code:

function get_all(){
    $this->load->database();
    $query = $this->db->get('test'); //test is my table 
    return $query->result();
}

Or, load database in your __construct method.

Or, IMO, It's better to autoload database by changing application/config/autoload.php, example is below.

$autoload['libraries'] = array('database','form_validation'); //form_validation is for example only
Sign up to request clarification or add additional context in comments.

4 Comments

If this was the case, the error would have been something like "undefined property 'db'..." or failure to load the model.
@Madmartigan I don't think so, PHP interpreter will exit after not finding get, because it's a Fatal Error, will not provide a Notice (if db is a an undefined property or not).
I tried locally to trigger the error but couldn't figure out how to not have the database loaded... but this was in a bloated CMS so there was a lot of stuff cross-loading, I couldn't figure it out. I assumed CI would be able to catch this and use show_error()...
Ah I see, the error could be due to declaring $db in the Model. Then db would be defined, just not an object.
3

In CodeIgniter, you should load database model before you use it. Use $this->load->database(); to load database model.

1 Comment

Thanks, this is what I was missing.
2

Your error is actually quite simple:

return $query->result;

Should be:

return $query->result();

Sometimes the line number reported by a PHP error isn't exactly the one you think it is, the parser is just doing it's best and reporting where it found an error.

There's one more issue:

$res = $this->my_model->getAll();

Should be:

$res = $this->my_model->get_all();

You have called your own function by the wrong name.

4 Comments

I think the error is in this $query = $this->db->get('test');.
@insane-36: If you are posting in a hurry, maybe you are coding in a hurry too. Try to slow down a bit and pay attention. Can we see the entire model class and controller method code?
No harm done, glad you got it working. Just use the autoload.php - no need to manually load the database class in each model.
Hmm, the autoload should have worked. I'm not too sure, maybe there's another issue. Like I said in the comment on Usman's answer, I'm testing and can't figure out how to get the db class to not load :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.