Php 如何将容器中的自定义条令存储库注册为服务?(框架不可知论)

Php 如何将容器中的自定义条令存储库注册为服务?(框架不可知论),php,doctrine-orm,containers,Php,Doctrine Orm,Containers,注意,我没有使用Symfony框架。我正在使用symfony容器组件和ORM。我已经创建了一个自定义回购协议,并希望将其注册为服务。我没有使用任何框架 class FooRepository extends EntityRepository { //... } 这是我在容器里搞砸的东西: <services> <!--<service id="doctrine" class="Doctrine\ORM\Repository\DefaultReposito

注意,我没有使用Symfony框架。我正在使用symfony容器组件和ORM。我已经创建了一个自定义回购协议,并希望将其注册为服务。我没有使用任何框架

class FooRepository extends EntityRepository
{
    //...
}
这是我在容器里搞砸的东西:

<services>
    <!--<service id="doctrine" class="Doctrine\ORM\Repository\DefaultRepositoryFactory" />-->
    <!--<service id="entity_manager" class="Doctrine\ORM\EntityManager"/>-->


    <service id="doctrine" class="Doctrine\ORM\EntityManager"/>
    <service id="app.org_1_repository" class="Doctrine\ORM\EntityRepository">
        <factory service="doctrine" method="getRepository"/>
        <argument type="string">Foo\Bar\Entity\MyEntity</argument>
    </service>
</services>
错误:未捕获异常“ReflectionException”,消息为“访问类Doctrine\ORM\EntityManager的非公共构造函数”

这个错误是有道理的。我想我更接近于现在评论的DefaultRepositoryFactory方法,但也无法让它发挥作用


通过大量的研究,解决这个问题还不是很清楚。99%的答案与Symfony有关。

我通过在容器中定义实体管理器解决了这个问题,如下所示:

<service id="doctrine.xml_configuration">
    <factory class="Doctrine\ORM\Tools\Setup" method="createXMLMetadataConfiguration" />
    <argument id="doctrine.xml_config_path" type="collection">
        <argument>path/to/mappings</argument>
    </argument>
    <argument id="is_dev_mode">dev_mode</argument>
</service>

<service id="doctrine.em">
    <factory class="Doctrine\ORM\EntityManager" method="create"/>
    <argument type="collection">
        <argument key="driver">your_driver</argument>
        <argument key="host">your_host</argument>
        <argument key="dbname">your_db</argument>
        <argument key="user">your_user</argument>
        <argument key="password" type="string">your_password</argument>
    </argument>
    <argument type="service" id="doctrine.xml_configuration"/>
</service>

现在,我可以根据需要使用entity manager。

问题不在于存储库,而在于您对entity manager服务的定义。创建实体管理器需要一点努力:您必须弄清楚如何将必要的配置转换为服务定义。Symfony框架在编译过程中使用了一些代码,但有点作弊。