I’m new using Doctrine2, where is the best place to write my custom function in the Entities or in the Repositories? let’s say I have an User entity and I want to get his age.
If I put the function getAge in my entity is very straight forward. But if I put the function getAge in my repository I have to pass the userId to get the age of that user. am I wrong? what is the best approach to achieve that?
Normally when working with entities, you’ll want to keep all data and operations on an entity in the entity itself, while repositories are only used for fetching the correct entity to begin with.
As an example,
getAge()andsetAge()return/operate on data in a single entity, so they should go in the entity itself, while a theoreticalgetUsersByAge()that locates all users of a specific age would go in the repository.