【问题标题】:Set Embeddable Field as Primary Key from Parent Entity Mapping - Doctrine2从父实体映射中将可嵌入字段设置为主键 - Doctrine2
【发布时间】:2014-10-02 22:42:03
【问题描述】:

TLDR; Doctrine2:我需要知道是否可以将 Embeddable 中的字段从父实体(或 MappedSuperclass')映射中创建为主键。我已经知道如何从 Embeddable 的映射中设置主键,但这并不理想(参见“长版本”)。

长版; 我正在尝试使用 Doctrine2 Embeddables 为我的实体创建身份值对象。

这是我的问题...

  • 我在一个实体 (MyEntity) 中有两个不同的嵌入对象(MyEntityId 和 OtherEntityId)。

  • 我希望 MyEntityId 中的一个字段成为 MyEntity 的主键。

  • 由于我在同一个实体中有两个可嵌入标识,因此我想在实体映射文件中定义主键字段,而不是可嵌入映射。

  • 如果我从可嵌入对象中定义主键,当我想对 OtherEntityId 执行相同操作时会遇到问题(因为我在其他地方使用它)。

  • 在 MyEntityId 和 OtherEntityId 中映射主键会导致 MyEntity 具有我不想要的复合键。

这是我目前的映射...

MyEntity:
    embedded:
        MyEntityId:
            class: 'MyEntityId'
            columnPrefix: false
        OtherEntityId:
            class: 'OtherEntityId'
            columnPrefix: false

MyEntityId:
    type: 'embeddable'
        id: 
            id:
                column: 'MyEntityId'
                type: 'string'

OtherEntityId:
    type: 'embeddable'
        id: 
            id:
                column: 'OtherEntityId'
                type: 'string'

解决方案?

  • 创建两个单独的 Embeddables 来表示相同的 Id 值对象(不是很干也不太复杂)

  • 从实体映射 Embeddable 的主键字段(这可能吗?我在文档中的任何地方都找不到)

【问题讨论】:

  • 您找到解决方案了吗?我也有同样的问题……
  • 仍然没有解决方案。我遇到了同样的问题。最好从映射它的实体中指向 Embedded 对象的列 id,以使 Embedded 字段成为 DB 表中主键的一部分。

标签: php doctrine-orm embeddable


【解决方案1】:

根据 Doctrine 2 文档:

Embeddables 只能包含带有基本@Column 映射的属性。 http://doctrine-orm.readthedocs.org/en/latest/tutorials/embeddables.html

我认为您无法将 @id 属性指定为可嵌入的,只是基本的普通字段,因为它们是某种容器而不是实体。

【讨论】:

  • 正如我在问题中所述,我可以在嵌入对象上指定 @id 属性。你的回答是针对我没有问过的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多