CodeIgniter: Can't load database from within a model

筅森魡賤 提交于 2020-02-15 18:33:35

问题


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?


回答1:


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

<?php

class userdb extends Model {

function __construct() {

    parent::Model();

    $this->load->database();

}
?>



回答2:


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.



来源:https://stackoverflow.com/questions/2686814/codeigniter-cant-load-database-from-within-a-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!