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

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

问题描述

可能有一个与Symfony 2和Doctrine 2中的实体不相关联的自定义存储库?我想把一些不适合其他存储库的本地SQL(它可以引用抽象或实体层次结构)放在一起。



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

解决方案

可能有你想要的那么多的存储库。但是,只有一个存储库可以与实体管理器链接。



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

 <! - 我的自定义存储库 - > 
< service id =acme.repository.my_entityclass =Acme\FQCN\MyEntityRepository>
< argument type =serviceid =doctrine.orm.entity_manager/>
< argument type =serviceid =acme.metadata.my_entity/>
< / service>

<! - MyEntity metadata - >
< service id =acme.metadata.my_entityclass =Doctrine\ORM\Mapping\ClassMetaData>
< argument> Acme\FQCN\MyEntity< / argument>
< / service>

存储库类必须从 EntityRepository

 命名空间Acme\FQCN; 

使用Doctrine\ORM\EntityRepository;

class MyEntityRepository扩展EntityRepository
{
/ **
*如果要注入任何自定义依赖关系,您必须要
*将它们添加到构造或创建设置器。我建议使用setter
*,在这种情况下,您不需要使用此类中的构造函数。
*
* public function __construct($ em,Doctrine\ORM\Mapping\ClassMetadata $ class,$ custom_dependency)
* {
* parent :: __ construct($ em,$ class);
*}
*
* /
}

不幸的是,您将无法通过教义服务检索它。相反,直接从容器中检索:

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






编辑 p>

如果您正在创建不应链接到任何实体的存储库,只需创建一个服务并注入必要的依赖关系。

 <! - 其他查询的存储库 - > 
< service id =acme.repository.miscclass =Acme\FQCN\MiscRepsitory>
< argument type =serviceid =database_connection/>
< / service>

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

 命名空间Acme\FQCN; 

使用\Doctrine\DBAL\Connection;

class MiscRepository
{
protected $ conn;

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


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).

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="Acme\FQCN\MyEntityRepository" >
    <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="Doctrine\ORM\Mapping\ClassMetaData">
    <argument>Acme\FQCN\MyEntity</argument>
</service>

The repository class would have to inherit from EntityRepository.

namespace Acme\FQCN;

use Doctrine\ORM\EntityRepository;

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, Doctrine\ORM\Mapping\ClassMetadata $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');


EDIT

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="Acme\FQCN\MiscRepsitory">
    <argument type="service" id="database_connection" />
</service>

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

namespace Acme\FQCN;

use \Doctrine\DBAL\Connection;

class MiscRepository
{
    protected $conn;

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

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

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