Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should objects in ORM contain operations other than CRUD?

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 users
  • getAllUsers() //get all users
  • promoteUser() //change the user's 'rank' (not permissions) within a website
  • addReputationPoints() //give the user x amount of reputation points

Would 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?

like image 550
F21 Avatar asked Dec 21 '25 07:12

F21


1 Answers

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

like image 72
Willem Mulder Avatar answered Dec 22 '25 20:12

Willem Mulder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!