5

First the Model class:

    class Xxxx_model extends Model
    {
      function XxxxModel()
      {
        parent::Model();
        $this->load->database();
      }

      function isInDatabase()
      {
        // Please ignore the sql query, it's just to show some random sql code with results
11.      $result = $this->db->query('SELECT * FROM someTable WHERE ...');
        $numberOfRows = $result->num_rows();
        ... 
        return $test;
      }
    }

Now the controller:

function someLogic()
{
  $this->load->model('xxxx_Model', 'xxxxModel'); // not necessary to specify
  $this->xxxxModel->isInDatabase();
}

When I run this I get the error:

Severity: Notice  --> Undefined property: Xxxx_model::$db .../xxxx_model.php line 11

I have no idea why this is. If I put the db code in the controller it seems to work, it's only with this setup in the model that it fails. I can't for the life of me figure out where the code is astray...

Stephane Grenier
  • 15,527
  • 38
  • 117
  • 192

4 Answers4

12

You have to load the db library first. In autoload.php add below code,

$autoload[‘libraries’] = array(‘database’);
Rikesh
  • 26,156
  • 14
  • 79
  • 87
uwublogs
  • 121
  • 1
  • 2
  • The person who asked the question has already used $this->load->database(); . So autoload is not required in my opinion. – Vishwas Jan 28 '21 at 09:31
6

add library 'datatabase' to autoload.

/application/config/autoload.php

$autoload['libraries'] = array( 'database' );

Propably you're started new project, like me ;-)

Tonci14
  • 61
  • 1
  • 1
5

To add to atno's answer:

class Xxxx_model extends Model
{
  function XxxxModel() //<--- does not match model name Xxxx_model
  {
    parent::Model();
    $this->load->database();
  }

Basically, you are not constructing the class or the parent class Model. If you are on PHP5, you may use __construct(), otherwise you must match the class name exactly, regardless of what alias you load it with in your controller. Example:

class Xxxx_model extends Model
{
  function __construct()
  {
    parent::__construct(); // construct the Model class
  }
}

I may be mistaken (haven't used 1.x in a while), but if you construct the Model class, there's no need to load the database if you are using the default connection settings in config/database.php, it should already be loaded for you.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • In fact, loading the database is definitely not needed if `$this->db` was available and working in the controller as mentioned, without having it loaded by the model as expected. – Wesley Murch Jun 20 '11 at 18:28
  • I can't believe I missed the constructor name! That's brutal. I've been on a 36+ hour coding spree because of a timeline. That's definitely one of the issues. I had to change the name of the model for it to be correctly loaded according to the docs, where I guess I missed also refactoring the name of the constructor. – Stephane Grenier Jun 20 '11 at 18:47
  • Btw, if I adjust the name of the constructor, I don't have to autoload the database anymore. Yeah :) – Stephane Grenier Jun 20 '11 at 18:47
  • 1
    @StephaneGrenier: Turn on some death metal and make some coffee to wake you up :) Also, definitely worth checking out the current 2.x some day, it's a very simple upgrade process and the docs are a bit better as well. – Wesley Murch Jun 20 '11 at 18:50
  • I was considering it, but I was also on a very tight deadline and just couldn't afford the time to upgrade. I will be looking into it shortly, see what's new and exciting other than just a version number increase ;) It's an awesome framework that's for sure. – Stephane Grenier Jun 20 '11 at 19:08
  • Aside, I don't recall ever having to construct the Model class in a CI Model... The 2.0 is not exciting really, but the huge thing for me was being able to use `$_GET` – Wesley Murch Jun 20 '11 at 19:15
2

If function XxxxModel() isn't your constructor, you're not loading the database by calling $this->xxxxModel->isInDatabase();

Try autoloading the database library from within autoload.php, or create a proper constructor in your model.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
afarazit
  • 4,907
  • 2
  • 27
  • 51
  • He would have had different errors if this was the case (i.e. class Model not found), but otherwise you are correct. – Wesley Murch Jun 20 '11 at 18:10
  • +1 to your edit - good catch. I'm sure OP will come back and claim it was a typo, I don't see the reason to hide the model name, Xxxxx is barely readable. – Wesley Murch Jun 20 '11 at 18:14
  • Isn't there a performance cost to autoloading it for each url if I only need it in a few places? Btw, that did solve it. – Stephane Grenier Jun 20 '11 at 18:25
  • 1
    Yes there is a tiny performance hit if you don't need database connection on every page. If you just need it only if you places you can create a `MY_DB_Model` class that will extend `Model` and include the database loading. Every time you need to fetch data from your DB extend that model, else CI's `Model`. Please mark the correct answer that solved your problem. – afarazit Jun 20 '11 at 18:29
  • @StephaneGrenier: If `$this->db` was working in your controller but the model was broken, you already had the database loaded... – Wesley Murch Jun 20 '11 at 18:30
  • I struggled on who to give the checkmark to because both worked, but the controller constructor name is why it wasn't working, the database autoload fixes it, but it's more of a workaround than the perfect solution. It's also more generic whereas the other one is more specific to this problem. Tough call... – Stephane Grenier Jun 20 '11 at 19:06