views:

97

answers:

2

Hello all,

I have been reading a few questions previously asked and I haven't come across one that answers my question in "black and white" for me! So, apologies if this is repetitive. The question is probably similar to asking, "how long is a piece of string" but bear with me!

For a registration system, I have a user model with functions such as:

  • add_user
  • delete_user
  • activate_user

The above user model deals with one table. The users table in the MySQL database.

You can guess what each function does but is this coarse enough? I mean should my model contain methods that are much broader such as:

  • add_record
  • delete_record
  • update_record

Where I pass in the table and a unique identifier of the record to delete, add or update?

I am using codeigniter, but I am interested in how things should be done in a pure MVC framework.

I apologise if this question is too picky.

Thanks all

+7  A: 

I'm not sure what you mean by "coarse".

"should my model contain methods that are much broader such as: add_record, delete_record, update_record"

Absolutely not. Never. That defeats the purpose of having a model.

That kind of "general-purpose" stuff is what a database is for. The point of a model is to adapt the general database to your specific problem.

Your model should be specific to your problem.

"user model with functions such as: add_user, delete_user, activate_user" That's the point. Your model reflects your application, your problem domain, your solution.

Your model should be able to -- in effect -- stand alone. You should be able to wrap your model in a command-line app or a GUI app or a web page.

S.Lott
+1 A lot of folk seem to think that all a model should do is CRUD. This means the controller needs to become involved in the business logic of your app. It's called a model because it models your problem - any actions your in your problem domain need a method on the model
meouw
@Meouw, thanks for saying that. I seem to think a model is a database abstraction layer but from your comments it seems the business logic should be there too! I misunderstood the model. Its strange though, why I use the controller to do form validation should I be doing that there?
Abs
validations should come in the model too, and report back to the controller is there is an error.
DGM
@DGM then the controllers job only becomes loading models/views and thats it?
Abs
@Abs: Correct. The controller is just a little bit of glue between presentation (or View) and Model. It attaches GUI actions to Model methods. It copies model values to Views for presentation. That's it.
S.Lott
Pretty much... it connects the request to the right model(s) and based on the model's response and the request parameters, connects it to the proper view.
DGM
A: 

You can guess what each function does but is this coarse enough. I mean should my model contain methods that are much broader such as:

* add_record
* delete_record
* update_record

Where I pass in the table and a unique identifier of the record to delete, add or update?

If you have a need to get only the information of a user doesn't necessarily make use an entire record, then it is just right for you to have *_user functions, but include it only in your user model.

If you have a need to get an entire records instead of just the user, then it is also just right for you to have *_record functions, but put it in your record model - NOT in your user model.

The thing to remember here is to not include all of those functions in just one model. Simply put, your user and is the same as a record.

Randell