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 follo开发者_C百科wing 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?
You're forgetting to call the parent constructor first. Something like:
<?php
class userdb extends Model {
function __construct() {
parent::Model();
$this->load->database();
}
?>
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.
精彩评论