开发者

CodeIgniter: Can't load database from within a model

开发者 https://www.devze.com 2022-12-27 22:23 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消