F21
F21

Reputation: 33391

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:

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?

Upvotes: 3

Views: 260

Answers (3)

Willem Mulder
Willem Mulder

Reputation: 13994

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

Upvotes: 1

Tadeck
Tadeck

Reputation: 137390

It depends on how you architecture your ORM solution.

My approach would be to enclose collective actions (made on multiple records at once) into some class representing eg. database table storing users or the one responsible for the basic ORM actions - depending whether they are table-specific or not.

Upvotes: 0

LeleDumbo
LeleDumbo

Reputation: 9340

In my most favorite framework (Kohana), we have:

ORM::factory('user')->where(<whatever condition required>)->find_all();

and the corresponding delete() (replacing find_all() above). Are you writing your own ORM library? If yes, then you can make operations that deal with more than one record as class methods instead of instance methods. It's up to your design, however. Kohana, for instance, still make it instance method.

Upvotes: 0

Related Questions