views:

1473

answers:

4

Hello, I'm testing codeigniter, trying to create a simple blog.

The video tutorial on codeigniter' site is nice, but very incomplete.

I'm not too familiar with the MVC structure, and am wondering exactly what goes in a "model".

For instance, I'm currently doing the "admin" part of the site : add a new entry, delete, modify, and so on.

The view only contains xhtml, and the controller takes care of the rest. What should be in the model ? Everything about the database (inserts, updates, selects) ?

+8  A: 

Depends who you ask.

Some people like to put as much as possible in the model (validation, data retrieval, etc), and have the controller just poke it to get the data it needs, which it then hands over to the view.

Think about it like this: if you have more than one controller accessing a single model, then shouldn't common things between them be in a common place (as long as that common thing actually has something to do with the model)?

nilamo
This is a good answer but I'd like to point out a good article on models here, as some additional reading: http://blog.astrumfutura.com/archives/373-The-M-in-MVC-Why-Models-are-Misunderstood-and-Unappreciated.html
Jani Hartikainen
@Jani: Terrific article, thanks for the link.
nilamo
The controller is only responsible for taking http parameters and turning them something into something the model can digest. The controller is the coupling agent between your client and the server. Doing it this way allows for your model code to be reused when you decide to server other clients using xml, json, or any other transport.
Juan Mendes
A: 

model = is object that "talking with your database" view = is object that building user interface controller = is the commander .. he got command from user and then he pass it on the model and serve to the user through view.

to create a simple blog, try to read Codeigniter getting started. it will help you a lot after you watch the video. the codeigniter references are good documented and well explained. try that first.

nightingale2k1
The model is not an implementation of the active record pattern. It's where the processing of data should go. It should also give you some way to connect to the db, yes, but it's sole purpose is not db access.
AntonioCS
talking with database not always in active record. I create manually my model to mimick active records (although it is so simple and not flexible). But model in MVC supposed to talk with DB althoug in CI it is not necesary (you can just use view and controller to do whatever you like)
nightingale2k1
Well I found that the codeigniter's introduction videos are fine to show what the framework can do, not so much to start a blog project.In the video they do not care about security and other important things, and they do not show how to use a model (which is why I'm here asking the question ^^;)
Manu
+1  A: 

The Model should contain everything database related, and perform all of the basic CRUD operations (Create, Get, Update, Delete).

The Controller should handle all communication between the model and the view. So for example, if you have a form for adding a new post, you should have a view for that form, which is called from a controller. The Controller would check to see if anything has been submitted, and if something has, call the create/insert method from the Post Model.

GSto
+1  A: 

For me, model is a where I do all 'dirty' work for my data. I fetch, insert, update data to database, all in a model. I create 1 model for 1 table in the db.

Controller will be logic central for a page that I build. It need as slim as possible. If a function go beyond 1 screen, then it's too long (except if it do form validation which is must be done in controller). This is where Model come to play. Controller just pass the data into model. I do checking, processing, and formatting the data in model. My controller then fetch processed data from model, pass it to view, finish.

Addition, model is not always have to do database related task. I also use model to fetch custom data from cookies. For me, that's is a data and model should process it befor I use it in controller/view.
This is my choice also. My Model does database functions and that is all. Libraries perform common functionality that isn't database related and controllers just collect data and deliver it to the View.
gamerzfuse