Welcome Guest, Not a member yet? Register   Sign In
Extending Main model and not the CI Model
#1

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!
Reply
#2

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.
Reply
#3

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.
Reply
#4

(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.
Reply
#5

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




Theme © iAndrew 2016 - Forum software by © MyBB