Tamás Pap
Tamás Pap

Reputation: 18303

Correct MVC pattern for User table

I have a User model, with properties (id, username, email, password) and methods (is_unique, get_by_id, save). As I understand the MVC pattern, my User model has to represent one given user. So, if I want to get a list of all users, should I implement a method for this in User model, or in a controller?

Upvotes: 0

Views: 259

Answers (3)

Kenny
Kenny

Reputation: 5410

You would have to build a UserCollection model, containing a collection of User models if you want to do it the proper MVC way. Your controller can never interact with the database directly, hence why we have to create a model ;)

Upvotes: 1

thwd
thwd

Reputation: 24848

Neither of both.

Create a new Model called something like userbaseor clientbase and use it to list users by criteria, retrieve information about your users in general (count, top 10, user of the month, etc...) and for operations that affect a group of users (that may have no direct relation one to another) like building a certain demographic collection of users to send them a mail.

Cheers

Upvotes: 1

Sjoerd
Sjoerd

Reputation: 75649

You should make a model class which represent a list of users, e.g. UserList.

Upvotes: 1

Related Questions