CodeIgniter Forums
How to extend CI_Model? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to extend CI_Model? (/showthread.php?tid=2544)



How to extend CI_Model? - El Forum - 08-11-2007

[eluser]mjijackson[/eluser]
What is the cleanest way to create a custom Model class that extends CI_Model? I tried creating a custom MY_Model file and sticking that in the application/libraries directory, but CI does not instantiate it like it does for other libraries. So then, if I try and load the model library first:

Code:
$this->load->library('model');

I get an error saying that the CI_Model class cannot be found. Any ideas?


How to extend CI_Model? - El Forum - 08-11-2007

[eluser]mjijackson[/eluser]
Problem solved. To extend Model:

Code:
class MY_Model extends Model
{
    
    function MY_Model()
    {
        parent::Model();
    }

}

And in the models:

Code:
require_once APPPATH . '/libraries/MY_Model.php';

class User extends MY_Model {}

Kind of a hack though...


How to extend CI_Model? - El Forum - 08-11-2007

[eluser]thurting[/eluser]
That is how it should be done.

Why do you say that is a hack? There is nothing wrong with using require().


How to extend CI_Model? - El Forum - 08-11-2007

[eluser]mjijackson[/eluser]
It's a hack because there should be a cleaner way to do it within the framework. That's one of the reasons why I use a framework, so it'll take care of the (potentially) messy inclusions for me.