在 Symfony 2/Doctrine 2 中拥有与实体无关的自定义存储库? [英] Having a custom repository not associated to an entity in Symfony 2 / Doctrine 2?

查看:18
本文介绍了在 Symfony 2/Doctrine 2 中拥有与实体无关的自定义存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在 Symfony 2 和 Doctrine 2 中拥有一个与实体无关的自定义存储库?我想在其中放入一些不适合其他存储库的本机 SQL(它可能指的是抽象或实体层次结构).

Would be possible to have a custom repository not associated with an entity in Symfony 2 and Doctrine 2? I would like to put in it some native SQL that doesn't fit well in other repositories (it may refer to abstract or entity hierarchy).

应该如何替换控制器代码 $this->getDoctrine()->getRepositoty(/* ??? */)?

How controller code $this->getDoctrine()->getRepositoty(/* ??? */) should be replaced?

推荐答案

您可以拥有任意数量的存储库.但是,只有一个存储库可以与实体管理器链接.

It's possible to have as many repositories as you wish. However, only a single repository can be linked with the entity manager.

您需要定义一些服务来添加自定义存储库.

You need to define a few services to add a custom repository.

<!-- My custom repository -->
<service id="acme.repository.my_entity" class="AcmeFQCNMyEntityRepository" >
    <argument type="service" id="doctrine.orm.entity_manager" />
    <argument type="service" id="acme.metadata.my_entity" />
</service>

<!-- MyEntity metadata -->
<service id="acme.metadata.my_entity" class="DoctrineORMMappingClassMetaData">
    <argument>AcmeFQCNMyEntity</argument>
</service>

存储库类必须从 EntityRepository 继承.

The repository class would have to inherit from EntityRepository.

namespace AcmeFQCN;

use DoctrineORMEntityRepository;

class MyEntityRepository extends EntityRepository
{
    /** 
     * If you want to inject any custom dependencies, you'd have either have to
     * add them to the construct or create setters. I'd suggest using setters
     * in which case you wouldn't need to use the constructor in this class.
     *
     * public function __construct($em, DoctrineORMMappingClassMetadata $class, $custom_dependency)
     * {
     *     parent::__construct($em, $class);
     * }
     *
     */
}

很遗憾,您将无法通过学说服务检索它.相反,直接从容器中检索它:

Unfortunately you'll not be able to retrieve it via the doctrine service. Instead, retrieve it straight from the container:

$this->get('acme.repository.my_entity');

<小时>

编辑

如果您要创建不应链接到任何实体的存储库,只需创建一个服务并注入必要的依赖项即可.

If you're creating a repository that shouldn't be linked to any entities, simply create a service and inject the necessary dependencies.

<!-- Repository for misc queries -->
<service id="acme.repository.misc" class="AcmeFQCNMiscRepsitory">
    <argument type="service" id="database_connection" />
</service>

由于您没有在自定义存储库中使用任何 Doctrine 的 ORM 功能,因此无需扩展 EntityManager.

Since you're not using any of the Doctrine's ORM features in a custom repository, there's no need to extend EntityManager.

namespace AcmeFQCN;

use DoctrineDBALConnection;

class MiscRepository
{
    protected $conn;

    public function __construct(Connection $conn)
    {
        $this->conn = $conn;
    }
}

这篇关于在 Symfony 2/Doctrine 2 中拥有与实体无关的自定义存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆