Inheritance on doctrine's embeddables

1.9k views Asked by At

Is it possible to use inheritance on value objects embedded in doctrine entities?

The situation I'm thinking about is:

I have an entity that has and embedded value object. That value object has the following hierarchy:

class myEntity {
    /** @Embedded(class = "baseValueObject") */
    private $value_object;
    ...
}

class baseValueObject {...}
class valueObject1 extends baseValueObject{...}
class valueObject2 extends baseValueObject2{...}

If I define my entity to have baseValueObject as an embeddable, nothing happens when I use the schema-tool to update my db schema, so I guess that's not the way to do it.

Another option that I'm thinking about is to use single-table inheritance on the entity to create a child entity that use one of the value objects, and another child entity for the other one. Like this:

class myEntity {
    /** @Embedded(class = "baseValueObject") */
    private $value_object;
    ...
}

class myEntityA extends myEntity {
    /** @Embedded(class = "valueObject1") */
    private $value_object;
    ...
}

class myEntityB extends myEntity {
    /** @Embedded(class = "valueObject2") */
    private $value_object;
    ...
}

class baseValueObject {...}
class valueObject1 extends baseValueObject{...}
class valueObject2 extends baseValueObject2{...}

What's the proper approach? Is it even possible to do it this way?

2

There are 2 answers

0
Inceddy On

If you want to extend one embeddable from another you need to set the parents properties as protected not private.

https://github.com/doctrine/doctrine2/issues/4097

0
Zbigniew Pretki On

If you want to use your Value Object in field then you should define new type in doctrine http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/custom-mapping-types.html

If you want inherit properties from base then you should use @MappedSuperclass annotation http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html

You should use @Embeddable when you want split entity by specific properties by creating specific entities. So you can't use Value Object as target. According to documentation (I'm not able to share third link).