Welcome Guest, Not a member yet? Register   Sign In
Models for Dummies
#1

[eluser]Unknown[/eluser]
I am new to CI development, and really loving the framework. I have some knowledge about the MVC pattern, and i understand (not fully) how to controllers and view work in CI.

However i have problems understanding the idea with Models, why are they important? I could load my queries in the controller, and serve a view with he SQL results.

I would really appreciate a "for-the-amateur" explanation for the Models, and even better a simple example how to build a simple model, and use it in the controller.

Thanks in advance!
#2

[eluser]Stefan Hueg[/eluser]
Hi and great to have you on board,
as you should already know MVC is the separation of control logic (controller), data sources (models) and presentation (views).

By doing so (and separating special parts into libraries and helpers) your code becomes reusable. The MVC Pattern forbids classes of the same type to communicate with each other (models -> models, controllers -> controllers etc...), so yeah you could put a database query in your controller but:

-it will never be reusable again
-you'll mess your code up

A model is a generic data source. Admittedly, most people use it for database access, but it is not limited to that. A model could also provide access to an API (like a wrapper), JSON data, XML data etc. and does all the work to return the data in a format which your controllers and views can work with.

It even makes collaboration more efficient.

I hope this makes it clear.
#3

[eluser]boltsabre[/eluser]
Stefan gave a good technical overview, but I once read a good example, restaurants with psychic powers:

Model = Kitchen/Chefs
Controller = Bar/Waiters
Views = Customers

Lets say in this hypothetical situation, waiters can actually read the mind of the customers, and know what they want/need.

The Waiter(controller) sees a new customer (page request) and knows what they want. They go to the kitchen and ask the Chef (model) to prepare it (for example, data from your DB). The Chef prepares it and gives it to Waiter. The waiter gets it and if it needs anything else (perhaps some bread, knife or fork or spoon, napkin, etc - different customers/page requests may want the same dish (model data), but with little different things attached) they can just add it quickly and then they go and give it to the customer (page displayed to user).

This is a good set up because the kitchen has many chefs (Models) that do different things (such as a salad chef, a fish chef, a steak chef, etc). Because the kitchen has many little individual chefs doing little individual things they can handle many requests/orders at once because that's all they have to do.

Sure, the Waiters COULD do what the models do, but then each waiter needs their own "kitchen" at the bar and needs to know how to prepare that dish, not very efficient. In fact, if it's just something simple (a glass of beer for example) then it makes perfect sense for a waiter (controller) to handle that instead of a chef (model). You could take it a step further and say that customers could do it themselves (the old school way of mixing PHP logic in with hmtl pages), but we know what a mess that is.

Hmmm, the example I saw was more articulate. Basically what I'm trying to say is it's a more efficient way of running things. Each entity (chef, waiter, customer) knows exactly what they have to do, and if it's beyond their ability, rather than give/code them that skill, you just go ask someone who knows how to do it.

The reason we have models, it to keep things separate and organised in the "back of the restaurant" just like you do in real life.
#4

[eluser]Stefan Hueg[/eluser]
Kudos boltsabre Smile




Theme © iAndrew 2016 - Forum software by © MyBB