How to configure dependency injection for repository class in symfony 3

后端 未结 3 443
温柔的废话
温柔的废话 2020-12-19 04:51

Symfony generator generated the following class of repository:

namespace AppBundle\\Repository;
use AppBundle\\Entity\\GroupEntity;

/**
 * GroupEntityReposi         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 05:31

    Recommended as of Symfony 3.3:

    As of Symfony 3.3 it is recommended to use the actual class name as service id (read this and this).

    AppBundle\Repository\GroupEntityRepository:
        factory: 'Doctrine\ORM\EntityManagerInterface:getRepository'
        arguments:
            - AppBundle\Entity\GroupEntity
    

    Original answer:

    You can configure your repository service like this:

    group_entity_repository:
        class: AppBundle\Repository\GroupEntityRepository
        factory: ["@doctrine.orm.entity_manager", getRepository]
        arguments:
            - AppBundle\Entity\GroupEntity
    

    You will probably never want to invoke the repository constructor yourself. Therefore this approach just uses the entity_manager to get the repository. The service container bascially uses this code to get the repository:

    $container->get('doctrine.orm.entity_manager')->getRepository('AppBundle\Entity\GroupEntity');
    

提交回复
热议问题