ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

MVC: how to use the model(s?)

June 09, 2011 5:39pm

Subscribe [4]
  • #1 / Jun 09, 2011 5:39pm

    BeginnerDev

    4 posts

    Hello,

    I have a basic question regarding the implementation of an MVC-framework to a website.

    I currently have a very basic site which i try to implement an MVC-framework on. I use a frontcontroller to redirect the requested pages, and for each page (=view) i have a separate controller.

    So let’s say my sitemap is:

    - Profile (this is a view)
    - News (this also)
    - Help (this also)

    Then, besides the frontcontroller i also have 3 pagecontrollers, each for it’s corresponding view/page.

    But let’s say each of the views interacts with a database, for example, the view ‘profile’ displays the users stored information (retrieve from database), while the view ‘news’ stores information inputted by user, into a database (insert into database).

    Now, should i also have one model for each controller/view ?? Or should i have 1 model which contains all the classes (for retrieving and inserting all kinds of data)? Or should i split my model in 2 models, one with all the classes for retrieve commands, while the other model for Insert commands???


    The reason i ask this is because currently, i have one model for each controller/view, so for example: for ‘Profile’, i have:
    -profileView
    -profileController
    -profileModel

    and for News i have:
    -newsView
    -newsController
    -newsModel

    etc. etc.

    Each element has it’s own view, controller AND model. Now when retrieving information from the database via a model, i found myself writing the same code for some models, since sometimes information has to be presented on different pages. Now if multiple models have almost the exact same pieces of code, the that wouldn’t be that efficient right?

    So hence my above question 😊.


    Much much thanks in advance!

  • #2 / Jun 09, 2011 9:39pm

    CroNiX

    4713 posts

    Generally 1 model per controller, to handle the database logic for that controller, although there is nothing wrong with using multiple models.  For instance, a user model might be needed in all controllers.

    As you point out, sometimes functionality will overlap.  Occasionally I will make a library that substitutes for a model that has common functions needed by various controllers.  Just instantiate the CI superglobal within the library and use the CI database calls as if it were a model.  Then just load, or autoload, that library and use it wherever.

  • #3 / Jun 09, 2011 9:45pm

    BeginnerDev

    4 posts

    Thank you very much mister CroNIX 😊.

    So basically, the way i was working was the ‘right way’ (having one model for each controller).

    I somehow didn’t think of making a library for functionality that’ll overlap, so i’m definately going to use that concept from now on, in case functionality overlaps!

    I was really confused and hesitating whether i was going the right route or not, so much much thanks for pointing me out in the right direction! 😊

  • #4 / Jun 10, 2011 5:02am

    InsiteFX

    6819 posts

    If you keep adding on to this you may want to check out the HMVC Modules in the WIKI.

    InsiteFX

  • #5 / Jun 13, 2011 10:20am

    Gbecaro

    42 posts

    here i have a base model where i do commom stuff (like insertion, recovery and removal) where i pass an object to the function and it does the trick, and for usages that are not commom to all i have a model for each controller.

  • #6 / Jun 13, 2011 10:33am

    BeginnerDev

    4 posts

    Thank you Summer Student for your reply! 😊

    I have that same concept right now, a base model ‘Model.php’ and seperate models which extend the basemodel. The base model contains general functions which apply to each child class, and each childclass contains page specific functions.

    So i guess i’m on the good way since you and others use this concept! 😊

  • #7 / Jun 14, 2011 5:01pm

    Gbecaro

    42 posts

    The base model i said earlier was a way we found on the company to reduce work from our programmers, based on our past projects we found that our programmers lost lots of time creating the same functions over and over to each use case and also this was creating some frankenstein functions on some cases… so the base model (and a base controller also) was created by what we found was the best options on functions that we use over and over by everyone… a nice help on productivity from the programmers…

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases