Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top