Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance on doctrine's embeddables

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?

like image 222
AntonioHS Avatar asked Jun 15 '15 13:06

AntonioHS


1 Answers

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

like image 76
Inceddy Avatar answered Nov 05 '22 02:11

Inceddy