Arnaud Janssens
Arnaud Janssens

Reputation: 150

What is the best Symfony 2 way to add the same method to multiple entities?

I have 10 entities. I want to create a method "getOneOrAll($id = null)" method that will belong to all of them.

It feels like a lot of waste of code to make an EntityRepository for every entities (so 10 entities + 10 empty repositories) which will extends a BaseRepository with my method (10 entities + 10 empty repositories + 1 abstract BaseRepository).

It feels also bad to copy/paste the method in all my entities. The only way i see is to extend my entities with a BaseEntity, is it the good one ? What is the best way to achieve that ?

Thanks.

Upvotes: 1

Views: 98

Answers (1)

Arnaud Janssens
Arnaud Janssens

Reputation: 150

I've found the solution by talking to people on Symfony IRC.

I will just create à BaseRepository which all my entities will use : 10 entities, 1 repository. If an Entity someday needs a particular method i'll just create its own entityRepository which will extends BaseRepository to keep the method.

Upvotes: 1

Related Questions