Reputation: 1
I was looking around for this problem a lot! But nothing realy helped to solve this issue.
I always get the following error: "Fatal error: Call to a member function query() on a non-object"
I have the following Controller:
<?php
class Station extends CI_Controller {
function __construct($params=array()) {
parent::__construct();
session_start();
$this->load->library('parser');
$this->load->helper(array('form', 'url'));
}
public function index() {
$this->tpl_data['location'] = $this->stationmodel->getLocations()->all;
$_SESSION['location'] = $this->tpl_data['location'];
$this->load->view('station', $this->tpl_data);
}
}
?>
And the following Model:
<?php
class StationModel extends CI_Model {
function __construct() {
parent::__construct();
}
public function getLocations() {
$query = $this->db->query("SELECT * FROM location WHERE City_IATA = 'MUC'");
var_dump($query);
// Just for test purposes!
foreach($query->result() as $row) {
echo $row->Location_Name_DE . '<br />';
}
}
}
?>
In my autoload.php I load the database module:
$autoload['libraries'] = array('database');
Also when I try to load the database modul directly in the controller I will get the same error.
My database.php:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '****';
$db['default']['database'] = 'comvel';
$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;
Anyone can help me out?
Thanks in advance!
Upvotes: 0
Views: 635
Reputation: 3055
This is a weird one. Seems like you've done everything right.
Have you tried removing the constructors altogether? My guess is that your constructor is somehow preventing your model from being initialized with the db
object. If you need to run some initialization code, it may be more appropriate to use a hook, such as the pre_controller
hook.
Upvotes: 0
Reputation: 1950
In your controller try removing $params=array()
from function __construct($params=array())
I would also remove the __construct() {}
from your model as it doesn't do anything and might cause you trouble.
Upvotes: 1