4

I'm trying to write my own MY_Model base class but I'm facing a weird problem:

/core/MY_Model.php

    function __construct() {
        if ( !empty($this->table)) {
            // query db, etc.
        }
        else {
            // even though that I set $this->table value 
            // in the child class, I always ended up here
            // it's always empty!!!!
            log_message('error', 'some error message');
        }
        // ...
    }
}

/models/test_model.php

    function __construct() {
        parent::__construct();
    }

    // ...

}

even though that I set $this->table value in the child class, I always ended up finding $table value empty in MY_Model class, it's always empty!!!! any hint please?!

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Steph
  • 149
  • 1
  • 2
  • 14
  • As far as I see you try to override property `$table`, right? I'm asking just to make clear if I understand your question well. – Leri Jun 04 '12 at 13:06
  • PLB: Not exactly, I'm trying to pass table name to the parent class to make it query database for some results. I thought redefining `$table` property in child class will do that. But it's not updating parent class $table property. If do a `var_dump()` in parent constructor $table property is still empty. thank u. – Steph Jun 04 '12 at 13:09
  • That's weird, I do the exact same thing in my MY_Model but $table is set as it should in the parent class... – nebulousGirl Jun 04 '12 at 13:12
  • @Stephanie strange. I've even tested your script, because it should be working and it really does what you expect. – Leri Jun 04 '12 at 13:14
  • weireddddddddddd! thank you so much anyway ;) – Steph Jun 04 '12 at 13:49
  • do you call the CI_Model constructor in your MY_model one ? – greenLizard Jun 04 '12 at 14:03
  • @StephanieLuther You're welcome. Let me know if you've solved problem. I'm interested what was causing that. – Leri Jun 05 '12 at 12:34

3 Answers3

11

Unless you need something specific/special - you should just use Jamie Rumbelows "MY_Model" - it will probably be better than anything you could write (or me) - and allows you to focus writing other code. No need to re-invent the wheel.

You can get the code from GitHub here

And here is an awesome tutorial that shows you how to use the MY_Model in your application.

Honestly - give it a go - you wont regret it.

Laurence
  • 58,936
  • 21
  • 171
  • 212
2

In your model constructor, do this:

class Test_model extends MY_Model{
   public function __construct()
   {
       parent::__construct();
       $this->table = 'test_model';
    }
    //...
}
WebNovice
  • 2,230
  • 3
  • 24
  • 40
1

You shouldn't try to override the $table variable in the child class. What you're doing is:

  • CI_MODEL contains:
    • protected $table, of own scope.
  • MY_Model contains:
    • protected $table, of own scope. This overrides the inherited variable $table.

When you override it in the child class, it won't bubble up to the parent class, cause the $table will become a part of the child class only.

What you need to do is remove the declaration of the $table variable in the child:

/models/test_model.php
<?php
class Test_model extends MY_Model {
    function __construct() {
        parent::__construct();
        $this->table = 'test_models';
    }

    // ...

}
Kao
  • 2,242
  • 3
  • 22
  • 31