Symfony 条令如何扩展自定义存储库并从条令实体管理器调用扩展存储库
我想知道如何扩展自定义存储库,并从doctrine中的doctrine实体管理器调用扩展存储库 我的实体类:Symfony 条令如何扩展自定义存储库并从条令实体管理器调用扩展存储库,symfony,doctrine-orm,Symfony,Doctrine Orm,我想知道如何扩展自定义存储库,并从doctrine中的doctrine实体管理器调用扩展存储库 我的实体类: /** * @ORM\Entity(repositoryClass="Vendor\MyBundle\Repository\MyEntityRepository") */ class MyEntity { ... 我的实体存储库类: class MyEntityRepository extends EntityRepository { ... class MyExtendedE
/**
* @ORM\Entity(repositoryClass="Vendor\MyBundle\Repository\MyEntityRepository")
*/
class MyEntity
{
...
我的实体存储库类:
class MyEntityRepository extends EntityRepository
{
...
class MyExtendedEntityRepository extends MyEntityRepository
{
...
我的扩展存储库类:
class MyEntityRepository extends EntityRepository
{
...
class MyExtendedEntityRepository extends MyEntityRepository
{
...
MyEntityRepository的号召:
class MyEntityManager
{
protected $emr;
/**
*
* @return MyEntityRepository
*/
public function getRepository()
{
return $this->emr->getRepository('MyVendorBundle:MyEntity');
}
...
调用MyExtendedentialRepository
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()与实体一起工作,并解析与存储库相关的问题。我不明白什么是逻辑或用例,但如果您需要重用存储库的某些部分,请抛出其他实体,建议使用traits。另一方面,如果您确实需要使用该场景,您只需在
getRepository
方法中构建myextendentityrepository
/**
*
* @return MyExtendedEntityRepository
*/
public function getRepository()
{
$class = $this->emr->getClassMetadata(MyEntity::class);
return new MyExtendedEntityRepository($this->emr, $class);
}
您不能,getRepository()与实体一起工作并解析与存储库相关的内容。我不明白什么是逻辑或用例,但如果您需要重用存储库的某些部分,请抛出其他实体,建议使用traits。另一方面,如果您确实需要使用该场景,您只需在
getRepository
方法中构建myextendentityrepository
/**
*
* @return MyExtendedEntityRepository
*/
public function getRepository()
{
$class = $this->emr->getClassMetadata(MyEntity::class);
return new MyExtendedEntityRepository($this->emr, $class);
}
它起作用了!。用例是,如果启用了可选捆绑包,它必须使用所需捆绑包的扩展存储库方法进行特定查询。使用此解决方案,启用可选捆绑包时无需手动添加特性。它可以工作!。用例是,如果启用了可选捆绑包,它必须使用所需捆绑包的扩展存储库方法进行特定查询。使用此解决方案,启用可选捆绑包时无需手动添加特性。