acanimal
acanimal

Reputation: 5020

Symfony2 custom Entity Repository acting as DAO?

Question is simple (answer maybe not): Is it a good practice to use a custom repository for an entity A as a DAO for that entity ?

Repositories holds specific queries related to that entity but is it good idea to add methods for persist it?

I come from Spring world and I'm trying organize my code in a similar way (controller->service->dao->entity) but I don't know if it is the right way in the Symfony2 world.

Thanks.

Upvotes: 1

Views: 1182

Answers (1)

m0c
m0c

Reputation: 2191

I am not 100% sure why you would need an DAO. Since the Entity Manager plays the role as data access object. I am not sure which kind of functionality you want to add to the repository. If you only want some persisting features, just use the entitymanager. If you want more logic within your persist function you can introduce manager classes.

Here is how I usually work:

I have ONLY queries in my repository classes. And I introduced another service layer that I call manager classes. Within those I have overall methods for changing my entities as well as persisting them.

Example: I have a User - UserGroup situation. I am Using the UserManager from the FOSUserBundle, but have also introduced a UserGroupManager. This UserGroupManager has functions that allow me to add Users to Groups (join) or to remove them, send Invites to groups etc. as well as trigger some events when I perform those actions. I also have a UserGroupRepository in which I use to find Groups that belong to specific persons or to join all the necessary data I usually need with my groups.

Upvotes: 1

Related Questions