Answer the question
In order to leave comments, you need to log in
Is using doctrine justified in a symfony 3 helper class?
I want to write to write a function that gets records from the database by id. And then use it in all controllers. I'm going to place this function in a helper class that will be located here AppBundle\Utils; But my function in the helper (or rather, even the entire helper class) will not have immediate access to doctrine.
Here is what they write on stackoverflow:
The problem here is that your Helper class is not container-aware; that is, it has no idea about all the services Symfony has loaded
that you're injecting the entire service container into the Helper class. That's not a best practice -- you should only inject what you need
Answer the question
In order to leave comments, you need to log in
Use your Repository class, in the constructor of which shove EntityManager, then your services will be able to access this repository. This is how you isolate the doctrine.
container-aware - knows about the container and can work with it, which, for good, it should not be able to do.
Records from a DB or entity? Firstly, this is a very big difference, and secondly, there is already the good old $em->find(Entity::class, $id), default param converters, etc.
In general, this is a very bad idea. And about the fact that there will be a global function that will deal with this, and the fact that you will push it as a static into a class and not a service, and the fact that Utils ... it's called "I was too lazy to come up with an adequate name."
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question