Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter: Can't load database from within a model

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?

like image 200
thedp Avatar asked Jul 31 '26 06:07

thedp


1 Answers

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

<?php

class userdb extends Model {

function __construct() {

    parent::Model();

    $this->load->database();

}
?>
like image 199
Jens Roland Avatar answered Aug 01 '26 20:08

Jens Roland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!