Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter MY_Model class

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?!

like image 355
Steph Avatar asked Dec 09 '22 23:12

Steph


2 Answers

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.

like image 79
Laurence Avatar answered Jan 02 '23 21:01

Laurence


In your model constructor, do this:

class Test_model extends MY_Model{
   public function __construct()
   {
       parent::__construct();
       $this->table = 'test_model';
    }
    //...
}
like image 37
WebNovice Avatar answered Jan 02 '23 20:01

WebNovice