开发者

Doctrine2: Mapping a parent class field from a child, how?

开发者 https://www.devze.com 2023-04-07 03:50 出处:网络
I\'m wondering if this can be done using Doctrine annotations: Say you have a parent class (a mapped superclass):

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 开发者_JAVA百科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!


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消