title: Codeigniter issue with controller and model
body: <p>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.</p>
  
  <p>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.)</p>
  
  <p>Here's the relevant controller (SiteObj.php) code:</p>
  
  <pre><code>class SiteObj extends Controller {
       function __construct() {
            parent::Controller();
            $this-&gt;load-&gt;model('Site_model');
            $data['query'] = $this-&gt;Site_model-&gt;create_site();
  
            if ($data-&gt;num_rows() == 1) {
               //etc. etc.
  </code></pre>
  
  <p>And here is the relevant Model (site_model.php) code:</p>
  
  <pre><code>class Site_model extends Model {
      function __construct() {
          parent::Model();
          $this-&gt;load-&gt;database();
      }
  
      function create_site(){
          $query = $this-&gt;db-&gt;query("SELECT * FROM sites WHERE siteid = '1' LIMIT 1");
  
          if ($query-&gt;num_rows() &gt; 0) {
           $row = $query-&gt;row();
           return $row;
               // etc etc
  </code></pre>
  
  <p>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.</p>
  
  <p>Here is the error message I am getting:</p>
  
  <p>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</p>
  
  <p>UPDATE: I followed the advice given by the first responder, but that didn't help.</p>
  
  <p>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:</p>
  
  <pre><code>Undefined property: SiteObj::$Site_model
  Fatal error: Call to a member function create_site() on a non-object
  </code></pre>
  
  <p>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.</p>
  
  <p><em><strong></em>**<em>*</em>*</strong> UPDATE 12/18 <strong><em>*</em>**<em>*</em>**<em>*</em>**<em>*</em></strong></p>
  
  <p>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:</p>
  
  <pre><code>$hook['pre_controller'][] = array(
          'class'    =&gt; 'SiteObj',
          'function' =&gt; '__construct',
          'filename' =&gt; 'siteObj.php',
          'filepath' =&gt; 'init'
      );
  </code></pre>
  
  <p>Next, I have a default controller handling all page calls. It's located at controllers/page.php and here's that code:</p>
  
  <pre><code>class Page extends Controller {
          // I am the core controller for the application.
          function _remap() {
              $mysite = new SiteObj();
          }
      }
  </code></pre>
  
  <p>This calls the init object that I have set up in application/init/siteobj.php. Here's that code:</p>
  
  <pre><code>class SiteObj extends Controller {
          function __construct() {
              parent::Controller();
              $this-&gt;load-&gt;model('Site_model');
              $data = $this-&gt;Site_model-&gt;create_site();
              if ($data){
                  $this-&gt;siteid = $data-&gt;siteid;
              } else {
                  $this-&gt;siteid = 0;
              }
          }
      }
  </code></pre>
  
  <p>Finally, here's the model code in models/site_model.php:</p>
  
  <pre><code>class Site_model extends Model {
          function Site_model() {
              parent::Model();
              $this-&gt;load-&gt;database();
          }
  
          public function create_site(){
              // I load the site data from the database and send the result to the controller
              $query = $this-&gt;db-&gt;query("SELECT * FROM sites WHERE siteid = '1' LIMIT 1");
              if ($query-&gt;num_rows() == 1) {
                  return $query-&gt;row();
              }
          }
      }
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Thanks in advance!</p>
  
  <p>Gary</p>