Doctrine2:如何从子类映射父类字段?
我想知道是否可以使用 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?
}
SomeEntity
和 SomeOtherEntity
都共享相同的父接口 (SomeEntityInterface
),但我不想只将映射超类 AbstractParent
上的 $foo
字段映射到此父接口(SomeEntityInterface
) 因为原则会产生性能开销(它会丢失用于映射层次结构中高层类的延迟加载)(即我不想使用单表或类表继承)。
使用 YML 解决方案很简单,因为即使它在父类上,您仍然可以映射 foo:
ConcreteChild1:
type: entity
oneToOne:
foo:
targetEntity: SomeEntity
所以
ConcreteChild2:
type: entity
oneToOne:
foo:
targetEntity: SomeOtherEntity
我必须使用 YML 还是我缺少一些东西可以让我通过映射 $foo
一个注释?
预先非常感谢,我知道这有点难以理解!
I'm wondering if this can be done using Doctrine annotations:
Say you have a parent class (a mapped superclass):
abstract class AbstractParent {
protected $foo;
}
which as two child classes:
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?
}
SomeEntity
and SomeOtherEntity
both share the same parent interface (SomeEntityInterface
) but I don't want to just map the $foo
field on the mapped superclass AbstractParent
to this parent interface (SomeEntityInterface
) as doctrine incurs a performance overhead (it loses lazy loading for mapping a class high in a hierarchy) (i.e. I don't want to use Single Table or Class Table Inheritance).
With YML the solution is simple as you can still map foo even though its on a parent class:
ConcreteChild1:
type: entity
oneToOne:
foo:
targetEntity: SomeEntity
and
ConcreteChild2:
type: entity
oneToOne:
foo:
targetEntity: SomeOtherEntity
So must I use YML or is there something I'm missing that would allow me to map $foo
through an annotation?
Thanks greatly in advance, I know this is a bit of a hard one to follow!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,这取决于。您是否使用注释执行在 YML 中所做的操作,只需在每个具体类中定义 $foo 并在那里对其进行注释,就像在 YML 中所做的那样。
如果 $foo 始终指向相同类型的实体,则可以使用 @MappedSuperclass 在你的抽象基类上,然后你在那里定义关系。
如果 SomeEntity 和 SomeOtherEntity 都是 SomeCommonFooAncestor 的子类,那么这也可以工作,在这种情况下,您可以使用 @MappedSuperclass 并说 AbstractParent 有一个
@OneToOne(targetEntity="SomeCommonFooAncestor")
。但是,有严重的性能考虑因素 对于 @OneToOne 和 @ManyToOne 关系采用这种方法(但你可能会同意)Well, it depends. Do you do what you're doing in YML with annotations, you'd just define $foo in each concrete class and annotate it there, just like you do in your YML.
If $foo always pointed to the same type of entity, you could use @MappedSuperclass on your your abstract base class, and then you define the relationship there.
That can also work if SomeEntity and SomeOtherEntity were both subclasses of SomeCommonFooAncestor, in which case you could use @MappedSuperclass and say that AbstractParent has a
@OneToOne(targetEntity="SomeCommonFooAncestor")
. However, there are serious performance considerations with that approach for @OneToOne and @ManyToOne relationships (but you might be okay with that)