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