1

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

1 Answer 1

2

Update based on updated code in original question:

Keep in mind that CodeIgniter's Hooks Class is initialized before the Loader Class, so your pre_controller hook is instantiating SiteObj and trying to call Site_model->create_site() before Site_model has been loaded via the Loader Class.

It's throwing an error because you can't call a method on an object that doesn't exist yet. In this case Site_model is the object that doesn't exist yet.

Remember that you can check your log files (located in /system/logs/) to see the order in which resources are executed. It might also be helpful to review the CodeIgniter Application Flow Chart.

Hope that helps!

End Update

The num_rows() method can only be used on the entire $query result object like you have in your Site_model:

$query = $this->db->query("SELECT * FROM sites WHERE siteid = '1' LIMIT 1");

if ($query->num_rows() > 0) {
        // do stuff
}

However, currently your create_site() method in your Site_model is returning a single row (return $row;) from the result object, and then you're trying to call num_rows() on that single row in your SiteObj controller.

There really is no need to do this because $query->row(); will always return a single result row.

If you did absolutely want to call num_rows() from inside your controller (again, there's really no point since $query->row() will always return only one row), you must return the entire $query result object like this:

$query = $this->db->query("SELECT * FROM sites WHERE siteid = '1' LIMIT 1");

if ($query->num_rows() > 0) {
    return $query;
}

Then in your SiteObj controller:

$this->load->model('Site_model');

$data = $this->Site_model->create_site();

if ($data->num_rows() == 1) {
    //etc. etc.
}

Hope that helps - if not let me know. It doesn't look like your code is too far off! For reference, check out the num_rows() method and the row() method.

Sign up to request clarification or add additional context in comments.

9 Comments

To anyone reading this, there was another answer posted that the OP is referring to in the original question.
Hi Colin, thanks for your feedback -- it's very similar to what the original responder said. But the issue seems to be a different one that I originally thought (see the UPDATE in my original post). Even when I take out all the database connection stuff I get this error: "Call to a member function create_site() on a non-object". Am I doing something wrong when I instantiate the object? Thanks!
I think I am onto something: it seems that when I take everything out of the construct function except the parent::Controller() or the parent::Model() call, things start behaving more as expected. Was that the issue? Can I not put any other code in that construct function?
@Gary: Yes, you can put other code in the constructor - that's not a problem. I quickly set up a CodeIgniter test instance with nothing more than the controller, model, and db table you mentioned in your question and everything is working fine. The only thing I changed was I removed the call to num_rows() in the controller. Is there more code you can post? Or can you post your most updated code?
@Gary: The error you reported in your update suggests you aren't loading your model anymore? You can't call create_site() without having first done $this->load->model('Site_model');. And yes, you can do that in the constructor.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.