Codeigniter Undefined property: xxxx_model::$db only from Model
You have to load the db library first. In autoload.php
add below code,
$autoload[‘libraries’] = array(‘database’);
add library 'datatabase' to autoload.
/application/config/autoload.php
$autoload['libraries'] = array( 'database');
Propably you're started new project, like me ;-)
To add to atno's answer:
class Xxxx_model extends Model{ function XxxxModel() //<--- does not match model name Xxxx_model { parent::Model(); $this->load->database(); }
Basically, you are not constructing the class or the parent class Model
. If you are on PHP5, you may use __construct()
, otherwise you must match the class name exactly, regardless of what alias you load it with in your controller. Example:
class Xxxx_model extends Model{ function __construct() { parent::__construct(); // construct the Model class }}
I may be mistaken (haven't used 1.x in a while), but if you construct the Model class, there's no need to load the database if you are using the default connection settings in config/database.php
, it should already be loaded for you.