php - CodeIgniter cannot load database library manually or autoload; Call to Member function get() on a non object
I'm unable to load the database library correctly manually in my model or in autoload.php
The line $query = $this->db->get('videos'); produces a Call to Member function get() on a non object error indicating the database library is not loaded.
I try loading the database library using the two lines commented out in the constructor for the model below but both return errors. If I uncomment $this->load->database I get a Call to member function database() on a non object error,
If I uncomment $this->load->library('database') I get a Call to member function library() on a non object error
<?php
class Videos_model extends CI_Model {
public function __construct()
{
parent::__construct();
//$this->load->database();
//$this->load->library('database');
}
public function get_videos()
{
$query = $this->db->get('videos');
}
}
The same line $this->load->database(); in the controller does not produce an error but the get() function error.
class Videos extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url'));
$this->load->database();
//$this->load->library('database');
$this->load->model('videos_model');
$this->videos_model->get_videos();
}
}
I also have set autoload in the config file,
$autoload['libraries'] = array('database');
in my CI Log file database driver is indicated to be initialized DEBUG - 2013-05-09 15:31:17 --> Database Driver Class Initialized
Here is my database config as well,
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'dbusername';
$db['default']['password'] = 'dbpassword';
$db['default']['database'] = 'dbname';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
I'm using CI 2.1.3 in eclipse with aptana studio 3 plugin and phpMyAdmin 3.5.8.1 and Apache Server 2.2
Update: I've confirmed that accessing the database works from the controller but not the model.
Answer
Solution:
Found the answer.
I had used this tutorial to get autocompletion working in Eclipse, http://www.taggedzi.com/articles/display/autocomplete-eclipse-codeigniter-2, (link 404'd) Once I removed the extra code in controller.php and model.php database access in the model was restored.
Here is a better tutorial on how to get autocomplete working in eclipse, http://www.web-and-development.com/codeigniter-and-eclipse-autocomplete/ (read the comments to find out how to get it working in models as well as controllers).