4
votes

I'm asking in context of symfony framework.

I am wondering if this is a good practice to use magic find methods (like find($id), findByField($value) etc...).

Those methods neither has return type nor are defined at all. This leads my IDE to mark warnings around them. Also I have to mark type of returned value all the time I use those methods, to make code completion working on those variables.

As a solution I am usually writing getters inside custom repository classes. In symfony docs there is example of such getter, that overload an variant of magic findBy method.

I have a bad feelings about such overloading magic find methods too, because it kind of mixes my own repo implementation with the parent EntityRepository one.

So I end up with writing custom getters that uses "get" prefix instead of "find".

Now, can someone tell me whats the best practice and why ?

EDIT

Recently i was looking for some ways to optimize doctrine, and I have found advise not to use magic finders, so its another argument against magic finders.

I have also read doctrine docs about magic finders and found that: http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/dql-doctrine-query-language.html#magic-finders

These are very limited magic finders and it is always recommended to expand your queries to be manually written DQL queries. These methods are meant for only quickly accessing single records, no relationships, and are good for prototyping code quickly.

So I have finally worked out my own opinion (and use case) for magic finders. Use them only for speed up coding, and always mark them with TODO, to rewrite them into custom repository methods while cleaning up code.

1
Use code annotation for this kind of tasks is the right way to proceed, in my opinion: redefining all (or some) methods is perfectly useless and could expose you to bug issues and something like this.DonCallisto
My personal opinion is that using these magic methods is bad practice. I find it far better to implement a concrete repository class for each entity. I even go a step further and will not extend the EntityRepository, but decorate it, so I am in complete control of the API of my repository classes.Gerry
I agree with @DonCallisto and @Gerry. If need be, I decide to user findBy()/findOneBy() rather than magics methods...Jovan Perovic
@Gerry - but what you acheive with decorated repositories ? For custom queries, you will have to expose its protected member _em, what doesn't make much sense for me. Also IMO its way better to extend repository and write custom getters with whole query building, as it allows you to optimize queries. If you have many relations between your entities, wrapping magic find methods, and later asking for related entities of the fetched one will generate multiple queries to database. With custom getter you can build query with joins good for the situation.Fisher

1 Answers

0
votes

In my opinion it's a bad practise too. I would suggest you to use findBy([]) and findOneBy([]) methods. I believe when I started learning symfony I had a case when magic methods didn't work at all because properties of my entity was named using underscores.