将可嵌入字段设置为父实体映射的主键 - Doctrine2

时间:2014-08-09 16:27:43

标签: php doctrine-orm embeddable

TLDR; Doctrine2:我需要知道是否可以在Parent Entities(或MappedSuperclass')映射中将Embeddable中的字段设置为主键。我已经知道如何从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'

方案吗

  • 创建两个单独的Embeddable来表示相同的Id值对象(不是非常干燥和太复杂)

  • 从实体中映射Embeddable的主键字段(这可能吗?我无法在文档中的任何位置找到它)

1 个答案:

答案 0 :(得分:0)

根据Doctrine 2文档:

  

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

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