2

I've written a new model for my CodeIgniter framework. I'm trying to load the database from within the constructor function, but I'm getting the following error:

Severity: Notice
Message:  Undefined property: userdb::$load
Filename: models/userdb.php
Line Number: 7

Fatal error:  Call to a member function database() on a non-object in 
/var/www/abc/system/application/models/userdb.php on line 7

Here is my model:

<?php

class userdb extends Model {

    function __construct() {

        $this->load->database();

    }
?>

What am I doing wrong here?

2 Answers 2

11

You're forgetting to call the parent constructor first. Something like:

<?php

class userdb extends Model {

function __construct() {

    parent::Model();

    $this->load->database();

}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Call to undefined method CI_Model::Model() in application/models/Xyz_manager.php on line abc
3

I'm not sure if it would cause a problem or not, but Model names are supposed to have the first letter capitalized. http://ellislab.com/codeigniter/user-guide/general/models.html Jens is also correct that you need to call the parent constructor as well.

3 Comments

That was my first thought as well - CodeIgniter naming conventions require capitalized class names. I was going to write that until I noticed the missing constructor call ;)
parent::Model(); did the trick. As for the Uppercase letter in the class/model name, I really hate it but I changed it to avoid possible errors. Thank you for your help.
How is this the best answer when it didn't even specified the correct answer? Jens below answered correctly.

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.