Let's say I have a User object for performing CRUD operations using ORM:
//Create a new User
$user = new User();
$user->name = "John Smith";
$user->age = 30;
$user->email = '[email protected];
$user->save();
Similiar operations would also be avaliable for Read, Update and Delete.
But what about cases such as these:
deleteAllUsers() //delete all usersgetAllUsers() //get all userspromoteUser() //change the user's 'rank' (not permissions) within a websiteaddReputationPoints() //give the user x amount of reputation pointsWould these type of operations go into the User class? If not, where should they go? Should I have a class called UserManager that deals with these operations?
Usually, the User class would extend an ORM class that provides the standard CRUD interface. The additional functions you talk about would very well fit within the User class itself.
It is always good practice to make 'fat models' and skinny controllers, i.e. put all logic that is directly related to data manipulation in the models, and only the 'overarching' logic in the controllers.
See e.g. here for more info http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With