CodeIgniter Forums
Extending Main model and not the CI Model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Extending Main model and not the CI Model (/showthread.php?tid=63250)



Extending Main model and not the CI Model - lazyfox - 10-11-2015

Hello Everyone!


Greetings!

I have a Main Model and it was extended to CI Model. I've created another model and extended to the Main Model and not to the CI Model. Is it okay to use like this? Please give some advice or recommendations.

Here are my codes:

Code:
//Main_model.php
class Main_model extends CI_Model {

//all common functions will be put here e.g., insert, update, delete ...

}


Code:
//Another_model.php
require 'Main_model.php';
class Another_model extends Main_model {

//all unique functions for every model will be coded here

}

Any comments and suggestions will be appreciated!

God bless and more power!


RE: Extending Main model and not the CI Model - JayAdra - 10-11-2015

Yes it is fine to extend your custom model, but you shouldn't load the model using:

PHP Code:
require 'Main_model.php' 

It's best to use CI's built-in loader, e.g.

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

in your constructor.


RE: Extending Main model and not the CI Model - slax0r - 10-12-2015

Loading the main model in the constructor will not work, because the constructor will not be executed due to a fatal error of "Main_model" class not found. What you could do is put the main model into the autoloader, but it seems stupid(ish) to have a useless main model object instantiated for nothing.

Why don't you create a MY_Model in application/core and have your models extend from that? That's what it was meant to be used for. And yes, it is ok to extend in such way, and will of course work, those are basics of OOP.


RE: Extending Main model and not the CI Model - JayAdra - 10-12-2015

(10-12-2015, 02:12 AM)slax0r Wrote: Loading the main model in the constructor will not work, because the constructor will not be executed due to a fatal error of "Main_model" class not found. What you could do is put the main model into the autoloader, but it seems stupid(ish) to have a useless main model object instantiated for nothing.

Why don't you create a MY_Model in application/core and have your models extend from that? That's what it was meant to be used for. And yes, it is ok to extend in such way, and will of course work, those are basics of OOP.

Yes, true - I didn't realise he was using Main_model instead of MY_Model - glazed over that one.


RE: Extending Main model and not the CI Model - lazyfox - 10-12-2015

Quote:
(10-12-2015, 07:42 AM)JayAdra Wrote:
(10-12-2015, 02:12 AM)slax0r Wrote: Loading the main model in the constructor will not work, because the constructor will not be executed due to a fatal error of "Main_model" class not found. What you could do is put the main model into the autoloader, but it seems stupid(ish) to have a useless main model object instantiated for nothing.

Why don't you create a MY_Model in application/core and have your models extend from that? That's what it was meant to be used for. And yes, it is ok to extend in such way, and will of course work, those are basics of OOP.

Yes, true - I didn't realise he was using Main_model instead of MY_Model - glazed over that one.


This is true, I can not call the "$this->load->model('...')" in my constructor because it was not defined yet.

Anyway, thanks for the quick reply. I just found out that I can create a customized model like 'MY_Model'.

God bless and more power!