Symfony 具有一个存储库的多个实体管理器

发布于 2025-01-14 04:48:24 字数 1877 浏览 4 评论 0原文

我有一个 symfony/api 平台应用程序,它连接到在 doctrine.yaml 中配置的 2 个不同数据库。我想注意到,两个数据库都存储相同的实体(但每个实体具有不同的操作),因此我们复制了所有实体类并为每个实体创建了一个存储库,以便迁移和 api 平台操作正常工作。由于这些实体共享许多通用存储库功能,因此到目前为止我已删除代码 重复是:

创建实体管理器装饰器

class AEntityManager extends EntityManagerDecorator{}
class BEntityManager extends EntityManagerDecorator{}

config/services.yaml

App\EntityManager\AEntityManager:
        decorates: doctrine.orm.a_entity_manager
App\EntityManager\BEntityManager:
        decorates: doctrine.orm.b_entity_manager
App\Repository\Main\AResourceRepository:
        arguments:
            - '@App\EntityManager\AEntityManager'
App\Repository\ProductPending\BResourceRepository:
        arguments:
            - '@App\EntityManager\BEntityManager'

创建一个基类(为每个实体)以在2个存储库

class RepositoryBase extends ServiceEntityRepository
{
  public function __construct(EntityManagerInterface $em, string $class) {...}

  // common methods
}

class ARepository extends RepositoryBase
{
    public function __construct(EntityManagerInterface $em)
    {
        parent::__construct($em, A::class);
    }
}

class BRepository extends RepositoryBase
{
    public function __construct(EntityManagerInterface $em)
    {
        parent::__construct($em, B::class);
    }
}

和实体

/**
@Orm\Entity(repositoryClass=ARepository::class)
*/
class A {
   string $prop;
}

/**
@Orm\Entity(repositoryClass=BRepository::class)
*/
class B {
   string $prop;
}

之间共享代码注意,第二个数据库出现在前缀为(比方说)/api/b/... 的端点中

我想知道是否有一种方法可以消除不同的存储库类并跨两个不同的实体定义相同的存储库。我的想法是根据请求的 url 更改用于注入 EntityManagerInterface 构造函数参数的对象,但我还没有找到有关它的具体内容,我不知道这是否可能。

I have a symfony/api-platform application which connects to 2 different databases configured in doctrine.yaml. I want to notice that both databases store the same entities (but with different actions on each), hence we duplicated all the entity classes and created a repository for each, in order for migrations and api-platform actions to work. Since those enities share a lot of common repository functionallity, what I have done so far to remove code
duplication is:

Create entity manager decorators

class AEntityManager extends EntityManagerDecorator{}
class BEntityManager extends EntityManagerDecorator{}

config/services.yaml

App\EntityManager\AEntityManager:
        decorates: doctrine.orm.a_entity_manager
App\EntityManager\BEntityManager:
        decorates: doctrine.orm.b_entity_manager
App\Repository\Main\AResourceRepository:
        arguments:
            - '@App\EntityManager\AEntityManager'
App\Repository\ProductPending\BResourceRepository:
        arguments:
            - '@App\EntityManager\BEntityManager'

Create a base class (for each entity) to share code between the 2 repositories

class RepositoryBase extends ServiceEntityRepository
{
  public function __construct(EntityManagerInterface $em, string $class) {...}

  // common methods
}

class ARepository extends RepositoryBase
{
    public function __construct(EntityManagerInterface $em)
    {
        parent::__construct($em, A::class);
    }
}

class BRepository extends RepositoryBase
{
    public function __construct(EntityManagerInterface $em)
    {
        parent::__construct($em, B::class);
    }
}

And entities

/**
@Orm\Entity(repositoryClass=ARepository::class)
*/
class A {
   string $prop;
}

/**
@Orm\Entity(repositoryClass=BRepository::class)
*/
class B {
   string $prop;
}

Note that all actions for the second database occur in endpoints prefixed with (let's say) /api/b/...

I would like to know if there is a way to eliminate the different repository classes and define the same repository across the 2 different entities. What I have in mind is to change the object which is used to inject the EntityManagerInterface constructor parameter based on the url of the request, but I haven't found something specific about it and I don't know if that's even possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文