【问题标题】:Doctrine Class Table Inheritance and property ignoralDoctrine 类表继承和属性忽略
【发布时间】:2014-06-17 19:35:45
【问题描述】:

我最初在这里发布了这个问题:https://github.com/doctrine/DoctrineBundle/issues/297

我有以下实体层次结构,MyProduct 是通过类表继承映射的父实体:

SyliusProduct    # Mapped superclass containing the 'options' association mapping
–- MyProduct     # Mapped superclass that should override the association (Head of CTI)
---- MyProduct1  # Ultimate children (entities)
---- MyProduct2
---- MyProduct3
---- MyProduct4

SyliusProductOption 实体具有多对多关联,该实体映射在 SyliusProduct 的映射中。

我无法更改 SyliusProduct 的映射(它是 Symfony 供应商的一部分)。

在生成模式时,doctrine 希望为每个最终子级生成 sylius_product_options 表,这会引发“表存在”异常。

有没有办法:

  • 通过创建 4 个不同的在最终子级映射关联 表(并指定不同的表名)?
  • 将其映射到 MyProduct 级别?
  • 直接忽略关联?

【问题讨论】:

  • 您是否为每个产品创建一个新实体?
  • 是的,MyProduct[1,2,3,4] 是扩展 MyProduct 的实体。
  • 您的问题是 Product 实体/表应该包含您在同一个实体/表中的所有产品,而不是每个产品中的单个产品。
  • 是的,没错,我的问题是“是否可以通过使用类表继承来存储子产品,同时将 'options' 关联保持在 MyProduct 级别?”
  • 在我看来,您正在以错误的方式查看数据库。为什么您需要将每个产品存储在一个单独的实体中,而不是将它们全部存储在一个表中?如果您想在生产线上添加另一个产品会发生什么情况,那么您是否需要创建另一个实体并更改继承以适应它?

标签: symfony doctrine-orm sylius


【解决方案1】:

这个问题很老了,但是你应该将你的产品实体继承类型设置为单表(不要设置子实体的表属性,否则你会得到一个错误,说表已经存在)

您还需要为每个子产品的 MyProduct 实体添加一个鉴别器映射,如下所示:

<entity name="MyProduct" inheritance-type="SINGLE_TABLE">
    <discriminator-map>
        <discriminator-mapping value="pr1" class="MyProduct1"/>
        <discriminator-mapping value="pr2" class="MyProduct2"/>
        <discriminator-mapping value="pr2" class="MyProduct3"/>
    </discriminator-map>
    ...
</entity>

如果父映射超类已经具有多对多选项映射,则不必在 MyProduct 实体中重新定义它。

这样,MyProduct1-4 实体将继承自 MyProduct,而 MyProduct 本身继承自 SyliusProduct,所有这些都在 SINGLE_TABLE 上,因此原则不会尝试为每个子产品创建具有相同名称的不同表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2022-01-25
    • 2016-08-29
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多