2017-05-04 46 views
0

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

我的实体类:

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

我的实体存储库类:

class MyEntityRepository extends EntityRepository 
{ 
... 

我的扩展库类:

class MyExtendedEntityRepository extends MyEntityRepository 
{ 
... 

MyEntityRepository召唤10

调用MyExtendedEntityRepository?

class MyOtherEntityManager 
{ 
    protected $emr; 

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

感谢

回答

1

你不能,学说getRepository()的工作原理与实体和解决有关存储库。我不明白什么是逻辑或用例,但是如果您需要重用某些存储库部分,则可以将其他实体推荐使用特性。另一方面,如果您确实需要使用该方案,则只需在getRepository方法中构建MyExtendedEntityRepository即可。

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

它的工作原理!用例是如果一个可选择的bundle被启用,它必须使得特定的查询重用所需bundle的扩展存储库方法。使用此解决方案时,无需在可选捆绑包启用时手动添加特征。 – Tsounabe