The following script comes from https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html#mapped-superclasses, and was only changed to include a second sub-class. It is my understanding that MappedSuperclassBase cannot exist by itself but must be extended by one and only one sub-class (i.e. either EntitySubClassOne or EntitySubClassTwo), and is the same concept as supertype/subtype for SQL. Agree?
How is a super/sub type defined using either YAML or XML instead of annotation mapping?
<?php
/** @MappedSuperclass */
class MappedSuperclassBase
{
/** @Column(type="integer") */
protected $mapped1;
/** @Column(type="string") */
protected $mapped2;
/**
* @OneToOne(targetEntity="MappedSuperclassRelated1")
* @JoinColumn(name="related1_id", referencedColumnName="id")
*/
protected $mappedRelated1;
// ... more fields and methods
}
/** @Entity */
class EntitySubClassOne extends MappedSuperclassBase
{
/** @Id @Column(type="integer") */
private $id;
/** @Column(type="string") */
private $name;
// ... more fields and methods
}
/** @Entity */
class EntitySubClassTwo extends MappedSuperclassBase
{
/** @Id @Column(type="integer") */
private $id;
/** @Column(type="string") */
private $name;
// ... more fields and methods
}
Based on our comments, I think I see your confusion. Because the docs handle both "MappedSuperclass" and "Discriminator" on the same page, I think you've mixed up their uses in your head. Hopefully this can help you:
MappedSuperclassprovides properties/defaults in a re-usable way, but it can never be an Entity by itself. This is comparable to PHP'sabstractclasses (which cannot be instantiated on their own)PersonEntity gives you 1 Entity. This Entity can be extended, for example byWorkerandManager.A good use-case for a
MappedSuperclasswould be anAbstractEntity. Every Entity needs an ID, a unique identifier. It also gives you something common to check against in Listeners and such. So, go ahead and create:See how this is both declared
abstractandMappedSuperclass?This is because neither (
abstract classandMappedSuperclass) cannot be instantiated on their own. You cannot do$entity = new AbstractEntity()because it's anabstractPHP class. Neither will Doctrine create a separate table forAbstractEntity.Next, create a
Person:The above,
Person, Entity is setup for Class Table Inheritance through theJOINEDinheritance type. Meaning that, on the database level, the tablepersonswill be separate from any columns added by other entities, extendingPerson.Notice how I did not declare
DiscriminatorMap. Below from the docs, highlighted in bold by me:Now, let's create a
Worker:So, now we've got:
AbstractEntity- is not a stand-alone EntityPerson- is a stand-alone EntityWorker- extendsPersonThings to note:
abstract class$worker instanceof Workerand$worker instanceof Person, because theWorkerextendsPerson. However,$person instanceof Workerwill befalse!Hope that managed to clear stuff up for you. If not, feel free to ask.