How to access repository methods for an entity in symfony2?

前端 未结 4 584
青春惊慌失措
青春惊慌失措 2020-12-15 22:10

I am stuck with a problem please help me with it. Here is the scenarario:

I have an entity \"User\" and corresponding repository \"UserRepository\", inside my entit

4条回答
  •  执念已碎
    2020-12-15 22:57

    Everything is possible however you should not access the entity's repository from the entity itself because of the separation of concerns.

    See this Stackoverflow answer for more details.

    Basically, the whole idea is that you want to have your application organized the following way.

    In short:

    Controller > Repository > Entities.

    It should not go in the other direction otherwise it creates a mess.

    If you want to go a bit further into the separation of concerns you could do the following.

    Controller > Service > Repository > Entities

    Alternative solutions:

    • Create a Twig extension that access a service (which access a repository) or a repository.
    • Create a method in your repository, call the method in your controller, map the data to IDs (keys of array are the IDs), pass the array to the template and then pull the data from the array using the entity IDs
    • Create a method in your repository, call the method in your controller, inject the data into your entities and access the data through the entity in your template.

    There are probably others but you would know better how your application is organized.

提交回复
热议问题