R
R
redguard2016-04-01 12:02:14
symfony
redguard, 2016-04-01 12:02:14

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

Is using doctrine justified in a symfony 3 helper class?
How to properly label such a function? In the controller?
And how to understand this "container-aware"? :) I translated it but I can't understand

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Evgenievich, 2016-04-01
@redguard

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.

S
Sergey, 2016-04-01
Protko @Fesor

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 question

Ask a Question

731 491 924 answers to any question