I am using codeigniter 1.7.3 and I have controllers set up and working, but I am having trouble getting back a query result when I try to access a model via a controller.
I have my database configured correctly in the config area (I know this because when I intentionally mess up one of the config options I get a message saying the database cannot be accessed.)
Here's the relevant controller (SiteObj.php) code:
class SiteObj extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Site_model');
$data['query'] = $this->Site_model->create_site();
if ($data->num_rows() == 1) {
//etc. etc.
And here is the relevant Model (site_model.php) code:
class Site_model extends Model {
function __construct() {
parent::Model();
$this->load->database();
}
function create_site(){
$query = $this->db->query("SELECT * FROM sites WHERE siteid = '1' LIMIT 1");
if ($query->num_rows() > 0) {
$row = $query->row();
return $row;
// etc etc
I have also tried naming the initial function Site_model instead of constructor, but with no luck. And I also tried auto-loading the database via the autoload.php file, and also by loading the database within the create_site function itself instead of the constructor function.
Here is the error message I am getting:
Fatal error: Call to a member function num_rows() on a non-object in /www/development/sunrise_ci/00ci00/application/init/siteObj.php on line 9
UPDATE: I followed the advice given by the first responder, but that didn't help.
I have since removed all references to the database connectivity in an attempt to isolate the issue. When I simply try to call the create_site() function from within the controller, I get this:
Undefined property: SiteObj::$Site_model
Fatal error: Call to a member function create_site() on a non-object
So it seems the issue is between the controller and the model, somehow they don't seem to be "talking" to each other correctly. Interestingly, I am able to see that I can pass a value through to the controller from create_site(), but I still get the error messaging along with it.
**** UPDATE 12/18 **********
Ok, first, I have amended the application/config/hooks.php file so that I can pre-load my init code ahead of all page calls. So this page has this:
$hook['pre_controller'][] = array(
'class' => 'SiteObj',
'function' => '__construct',
'filename' => 'siteObj.php',
'filepath' => 'init'
);
Next, I have a default controller handling all page calls. It's located at controllers/page.php and here's that code:
class Page extends Controller {
// I am the core controller for the application.
function _remap() {
$mysite = new SiteObj();
}
}
This calls the init object that I have set up in application/init/siteobj.php. Here's that code:
class SiteObj extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Site_model');
$data = $this->Site_model->create_site();
if ($data){
$this->siteid = $data->siteid;
} else {
$this->siteid = 0;
}
}
}
Finally, here's the model code in models/site_model.php:
class Site_model extends Model {
function Site_model() {
parent::Model();
$this->load->database();
}
public function create_site(){
// I load the site data from the database and send the result to the controller
$query = $this->db->query("SELECT * FROM sites WHERE siteid = '1' LIMIT 1");
if ($query->num_rows() == 1) {
return $query->row();
}
}
}
I get this error:
Undefined property: SiteObj::$Site_model
Fatal error: Call to a member function create_site() on a
non-object in
/www/development/sunrise_ci/00ci00/application/init/siteObj.php
on line 7
Thanks in advance!
Gary