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.

More on Models in MVC

December 19, 2009 4:11am

Subscribe [3]
  • #1 / Dec 19, 2009 4:11am

    nZac

    95 posts

    I don’t really know where this post would go so I figure, why not put it here?!?!

    I am looking for an advanced look into what a model specifically contains within the MVC architecture and how to build effective ones.  I am not quite an expert yet on CI (or PHP for that matter 😊 but I want a little more substance that what is given in the user manual.

    Furthermore to pick peoples brains on this idea.  Is it better to use Active record or Models? I am working on a large scale project with a lot of AJAX involved.  It is primarily a CRUD system but has some advanced business logic with-in it. 

    Any suggestions?  Books, Tutorials, Blogs, Screen casts… anything welcome!

    Cheers!

  • #2 / Dec 19, 2009 4:15am

    wiredesignz

    2882 posts

    Read some good reference material. http://www.phpwact.org/pattern/model_view_controller

  • #3 / Dec 19, 2009 5:46am

    skunkbad

    1326 posts

    I don’t think there is an advanced look at what models do. You just use models to get data to return to the controller. It’s a pretty simple concept. Usually that data is from a database, and you can use Active record in the model, which is where it should be used. You can try to be a purist and sort your code in a very strict manner, thereby making sure that no database queries, updates, inserts, or deletes happen outside of a model, but in the end you are generally going to be the one that maintains the code. If you’d rather do all of this in the controller, then that’s up to you. I think the benefit to using models is that they help organize your code. In fact, at least for me, MVC just means more organization. Yeah, you can extrapolate a bunch of facts about MVC, but in the end it’s just organization. Whatever way you choose to organize, just stick with it.

  • #4 / Dec 19, 2009 6:03am

    wiredesignz

    2882 posts

    The CodeIgniter developers are somewhat responsible for most of the community members being truly clueless about the proper role of the model in an application, this is probably due to their lack of convention and loose interpretation of the MVC design pattern.

    From what I can gather the original version of CI did not even have a model class.


    Models are much more than containers for database access, they should encapsulate the business logic for your application.

  • #5 / Dec 19, 2009 8:31am

    Johan André

    412 posts

    Interesting…

    One thing I thought of is that in some other frameworks (like rails) the validation is done in the model, but in all CI-exampels it’s done in the controller. I personally think the validation should occur in the model since it’s responsible for storing/adding/fetching the data.

    Any thoughts of this?

  • #6 / Dec 19, 2009 10:04am

    Jamie Rumbelow

    546 posts

    Interesting…

    One thing I thought of is that in some other frameworks (like rails) the validation is done in the model, but in all CI-exampels it’s done in the controller. I personally think the validation should occur in the model since it’s responsible for storing/adding/fetching the data.

    Any thoughts of this?

    It’s an interesting idea - but the nature of CodeIgniter’s validation system means that it makes more sense to validate in the controller. After all, CodeIgniter’s Form Validation library validates and links to the forms, whereas Rails uses the data. It works with Rails’ ActiveRecord because it’s an ORM, and CodeIgniter’s Model layer isn’t designed to be an ORM from the offset.

    If you look at some of the ORMs out there for CodeIgniter already there usually is some kind of validation mechanism.

    Jamie

  • #7 / Dec 19, 2009 1:54pm

    skunkbad

    1326 posts

    Read some good reference material. http://www.phpwact.org/pattern/model_view_controller

    I couldn’t read this whole page, as it is huge, and I have to get to work. Am I right that it is suggesting that the model would load the view? This is interesting if true, as I have always loaded views from my controllers. Is it saying that a controller simply looks at the request, and tells the model what to do based on the request? Nothing more, nothing less? I can imagine this would make for some really small controller files.

  • #8 / Dec 19, 2009 3:31pm

    nZac

    95 posts

    Thank you everyone for you conversation, I am getting more feedback than I expected.

    wiredesignz,  Thanks for the article it has a lot of great information and even more resource links for me!!

    skunkbad, Interesting thoughts about what the model is for.  I disagree with you but interesting none the less.  On another note.  I do not feel that the link in question is saying that the model should call a view and the first line of the controller sections says that:

    The controller receives and translates input to requests on the model or view.


    However I do understand where you got this idea:

    The controller is NOT a Mediator between the view and the model.

    This is how I have always looked at the design flow:

    - User accesses webpage to view the distance between two points on a map
    - Controller sends a request to model with all the information required to get the two points
    - The model then validates that they are indeed real points, fetches, validates the returned data and then sends it back to the controller
    - The controller then does the math of the latitude and longitude of the two points and returns that information back to the view.

    This process to me seems to organize the code the best mainly for maintainability reasons.


    Thoughts?

  • #9 / Dec 19, 2009 7:05pm

    wiredesignz

    2882 posts

    @Johan Andre & Jamie Rumbelow

    Input validation and Data validation should be considered as separate subjects.

    It makes sense to validate incoming $_POST data in the controller to ensure it is cleaned and meets a minimum criteria. ie: password length or valid email address format

    After which another level of validation should be carried out in the model to make sure the clean data is suitable for use by the application. ie: not a duplicate email address


    The Model should never be made aware of the controller or the input class.

  • #10 / Dec 19, 2009 7:22pm

    wiredesignz

    2882 posts

    @nZac

    The controller should not make any calculations, (business logic) at all, the model is totally responsible for creating the data set for the map and the view is able to get data from the model provided it does not change the state of the model. ie: does not alter the co-ordinates etc.

    EDIT:
    Models and libraries are explicitly passed to, and are available in, the CodeIgniter view, so this is possible.

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

ExpressionEngine News!

#eecms, #events, #releases