• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Same function in different model

Hi All,

I need to know something about handling the model in optimised way.

Suppose, I have a model (e.g. M1) with 30 functions inside it.  E.g. One of them is A(). I have another model (e.g. M2) with 4 functions inside it. I have loaded model M2 in my controller C.

Now I need to call function A() to do the required task in my controller C. What would be the best optimised way to do this? 

Way 1: Load model M1 also in my controller C and then calling the function A()
Way 2: Create the same function A() in model M2

Looking for reply

Create a MY_Model. See the CodeIgniter Users Guide.
What did you Try? What did you Get? What did you Expect?

Joined the CodeIgniter Community in 2009.          ( Skype: insitfx )


What about:

Way 3, load both models in your controller.
Way 4, move function A into your second model
Way 5, reduce the size of your model A by splitting the functions across two or three models.
Way 6, ....I am sure there are more options.

The best answer for you depends on what it is you are concerned about.

Writing the same function twice in two models is definitely not the way to go. No no no. If you do that you might as well copy the function straight into the controller and forget about using a model.

CI is blazingly fast. If you have a particular need for a controller to perform at a super speed, completely optimised for speed and nothing more, you might be better off writing in php and not using any framework at all. All frameworks have some overhead.

If you are optimising for any other reason, you would have to define what you meant by 'optimised'. Although you would have to have pretty special reasons for rewriting the same function (in your example function A) twice in two different files. Your code will quickly start to look like spaghetti code if you are not careful.

Another thing to look at is the use of libraries as a third layer. So the controllers call libraries which call models. Libraries hold the business logic, models hold the database logic.

Controller: Recieves request, checks authorisation, calls the relevant business logic, collects results and calls relevant views.
Library: Performs the business logic, calls the relevant models for data, manipulates the data, returns the result data.
Models: Recieves the request for data, interrogates the model, creates resulting data arrays, returns them to the library.

The beauty of CI, is that you can do it this way, or your way, or in any way you like. Unlike other frameworks, CI does not proscribe how you structure your app. So if you went with Way1 or Way2 in your example, or any other way, the choice is completely yours.

Best wishes,



Thanks for your reply guys!

I had a confusion about loading the models in controller. What I think is loading a model is just like including the script in normal php page.

So in my example, if I load both models M1 and M2, script length will be very high loading all the unnecessary functions which I do not want in my controller. Moving that function to M2 is also not possible because in other controller, A() is used from M1.

That's why I asked what is the optimised way of having the less script with quick execution of the codes (my actual concern). Or maybe, I am having the wrong idea about CI. Kindly elaborate to make me understand better.


If all your methods are in a single class, and you want to use a method, then you have to load the entire class. PHP is very efficient at this.

However, if you have a large model, you could split it into smaller models.

Alternatively, you can do as I suggested before and have a smaller model or library that you refer to in your controllers. It itself calls other models. So the only functions loaded are the ones required by that method. This might get a bit messy if your functions are not easily grouped into related functions.

I am not sure what the speed implications are of loading a model with 100 lines and a model with 1000 lines. I used to be concerned with the size of my models, but have never really seen any noticeable delay in loading larger models.

The loading of a large file server side is usually not the problem (unless they are exceedingly large and you start hitting buffer or memory limits). Nothing is executed in the loading of a file, it is usually the executing that takes the vast bulk of the time.

I would suggest that if your code is well coded for speed, avoiding those php functions that cause a lot of overhead, loading a large model will not be the cause of any delay. So I think you concern with loading the large model is probably not real. How large is your file anyway? I mean, is it truly enormous?

I am no expert in this at all, so others with more insight or experience of coding for pure speed with processor heavy functions will hopefully chime in too.

Best wishes,


PS I recently had a pdf library that really slowed everything down, sometimes timing out. I swapped libraries and everything was super fast again.

Hi Paul!

Thanks for your reply. I'll look forward in this.

Thanks again!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

Users browsing this thread:
1 Guest(s)

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.