Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity vs Repository (what's the difference)

I'm rather new to Doctrine. Using the Symfony2 framework I have created various entities that have basic getter and setter methods. To date, if I wanted some extra functionality I would just create a new method in the entity to do that. (So for example, if I wanted to store a user's password I would create a method to get the user's password and store the hashed value of the password directly).

I have now heard that there are such things as "repositories" that should hold "more complex" methods instead of storing them in the entity itself. Is this true/false also what is the actual benefit of having a repository if you can simple keep all the code in one place in the actual entity?

like image 959
John Crawford Avatar asked Jan 03 '14 19:01

John Crawford


1 Answers

Entity is an object representing (usually) a row in a db, you should put there methods (no matter how complex they are) that operate just with entity's inner state - they either return some data based on its properties, modify its properties, or both.

Repository is an object that is meant to fetch and save entities from/to storage - it represents db table. You should put there methods that have to interact with the storage, like save($entity), findActiveUsersOrderedByRegistrationDate(), etc.

like image 60
Matěj Koubík Avatar answered Oct 08 '22 23:10

Matěj Koubík