Injecting dependency into entity repository

后端 未结 6 1491
有刺的猬
有刺的猬 2020-12-13 04:46

Is there a simple way to inject a dependency into every repository instance in Doctrine2 ?

I have tried listening to the loadClassMetadata event and usi

6条回答
  •  一生所求
    2020-12-13 04:59

    This is a YAML version of Aldo's answer, just in case you are using YAML configurations instead of XML

    your_namespace.repository.repos_name:
        class: %your_namespace.repository.repos_name%
        factory: ["@doctrine", getRepository]
        arguments:
            - entity_name
            - entity_manager_name
        calls:
            - [setContainer, ["@service_container"]]
    

    And prior to version 2.8:

    your_namespace.repository.repos_name:
        class: %your_namespace.repository.repos_name%
        factory_service: doctrine
        factory_method: getRepository
        arguments:
            - entity_name
            - entity_manager_name
        calls:
            - [setContainer, [@service_container]]
    

    Also, as a note, entity_manager_name is an optional parameter. I want the default for my particular use, so I just left it blank (just in case I ever rename the default manager).

提交回复
热议问题