tags:

views:

214

answers:

3

What are the best practices for implementing models in the MVC pattern. Specifically, if I have "Users" do I need to implement 2 classes. One to manage all the users and one to manage a single user. So something like "Users" and "User"?

I'm writing a Zend Framework app in php but this is more a general question.

+5  A: 

The model should be driven by the needs of the problem. So if you need to handle multiple users, then a class representing a collection of Users might be appropriate, yes. However, if you don't need it, don't write it! You may find that a simple array of User objects is sufficient for your purposes.

Giraffe
+1: Use built-in collections as much as possible.
S.Lott
A: 

That's going to be application and MVC implementation specific. You might well define a class collecting logically related classes, or you could define a static register on the user class. This is more of an OO question than MVC.

annakata
A: 

I'll second Giraffe by saying the use of included collections is almost always better than trying to write your own.

But I think your original question could be reworded a little differently... "Do I need a separate class to manage users other than the User class?

I use a static factory class to build all of my users and save them back to the database again. I'm of the opinion that your model classes need to be as dumbed down as possible and that you use heavy controller classes to do all of the work to the model classes.

thaBadDawg