Doctrine2:从孩子那里映射父类字段,怎么样?

时间:2011-09-25 18:59:57

标签: php doctrine-orm symfony

我想知道是否可以使用Doctrine注释来完成:

假设您有一个父类(映射的超类):

abstract class AbstractParent {
    protected $foo;
}

作为两个子类:

class ConcreteChild1 extends AbstractParent {
    /**
     * @OneToOne(targetEntity="SomeEntity")
     */
    // How can I map this to foo above?
}

class ConcreteChild2 extends AbstractParent {
    /**
     * @OneToOne(targetEntity="SomeOtherEntity")
     */
    // How can I map this to foo above?
}

SomeEntitySomeOtherEntity都共享相同的父接口(SomeEntityInterface),但我不想只映射映射的超类{{1}上的$foo字段这个父接口(AbstractParent)因为doctrine会导致性能开销(它会丢失延迟加载以在层次结构中映射一个高级类)(即我不想使用单表或类表继​​承)。

使用YML,解决方案很简单,因为即使它在父类上也可以映射foo:

SomeEntityInterface

ConcreteChild1:
type: entity
oneToOne:
  foo:
    targetEntity: SomeEntity

所以我必须使用YML,或者是否有我遗漏的东西可以让我通过注释映射ConcreteChild2: type: entity oneToOne: foo: targetEntity: SomeOtherEntity

非常感谢,我知道这有点难以理解!

1 个答案:

答案 0 :(得分:0)

嗯,这取决于。你是否用注释在YML中做了什么,你只需在每个具体类中定义$ foo并在那里注释它,就像在你的YML中一样。

如果$ foo总是指向同一类型的实体,则可以在抽象基类上使用@MappedSuperclass,然后在那里定义关系。

如果SomeEntity和SomeOtherEntity都是SomeCommonFooAncestor的子类,那也可以工作,在这种情况下你可以使用@MappedSuperclass并说AbstractParent有一个@OneToOne(targetEntity="SomeCommonFooAncestor")。但是,对于@OneToOne和@ManyToOne关系有serious performance considerations方法(但你可能没问题)