教义如何扩展自定义存储库并从学说实体管理器调用扩展存储库 [英] Doctrine How to extend custom repository and call the extended repository from doctrine entity manager

查看:45
本文介绍了教义如何扩展自定义存储库并从学说实体管理器调用扩展存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何扩展自定义存储库并从学说的实体管理器中调用扩展的存储库.

I would like to know how to extend a custom repository and call the extended repository from doctrine entity manager in doctrine.

我的实体类:

/**
 * @ORM\Entity(repositoryClass="Vendor\MyBundle\Repository\MyEntityRepository")
*/
class MyEntity 
{
...

我的实体存储库类:

class MyEntityRepository extends EntityRepository
{
 ...

我的扩展存储库类:

class MyExtendedEntityRepository extends MyEntityRepository
{
 ...

调用MyEntityRepository:

Call of MyEntityRepository:

class MyEntityManager
{
    protected $emr;

    /**
     *
     * @return MyEntityRepository
     */
     public function getRepository()
     {
          return $this->emr->getRepository('MyVendorBundle:MyEntity');
     }
 ...

调用MyExtendedEntityRepository?

Call of MyExtendedEntityRepository?

class MyOtherEntityManager
{
    protected $emr;

    /**
     *
     * @return MyExtendedEntityRepository
     */
     public function getRepository()
     {
         //This is what i want to know: How to access to the extended repository?
     }
 ...

谢谢

推荐答案

您不能,doctrine getRepository()与实体配合使用并解析相关的存储库.我不了解什么是逻辑或用例,但是如果您需要重用存储库的某些部分,则抛出其他实体,建议使用特征.另一方面,如果您确实需要使用该方案,则只需在 getRepository 方法中构建 MyExtendedEntityRepository .

You can't, doctrine getRepository() works with the entity and resolve the repository related. I does not understand what is the logical or use case, but if you need reuse some part of repository throw other entities its recommended the use of traits. In the other hand if you really need use that scenario you can simply build MyExtendedEntityRepository in the getRepository method.

/**
 *
 * @return MyExtendedEntityRepository
 */
 public function getRepository()
 {
   $class = $this->emr->getClassMetadata(MyEntity::class);
   return new MyExtendedEntityRepository($this->emr, $class);
 }

这篇关于教义如何扩展自定义存储库并从学说实体管理器调用扩展存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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