Much asked on SO and around the web with regards to ZF2 with Doctrine 2 and using Discriminators is: how do you not declare all child Entities on the parent Entity? Especially when you have multiple modules?
The short answer is: do not declare a discriminatorMap
. Doctrine will handle it for you.
The longer answer is below.
A popular article on how to be able to declare your child Entities, on the children Entities, instead of the parent, is this one.
However, Doctrine 2 has changed somewhat since it was written, e.g. the
AnnotationWriter
no longer exists.There, however, is a simpler way, as I mentioned in the question: do nothing.
To now use Discriminators using the “Class Table Inheritance” method (as opposed to “Single Table Inheritance”) is to NOT DECLARE a Discriminator Map! (Not sure if this will also work for STI…)
I found an old ticket on Github that explains the same issue as this answer and which many people still have, that declaring on the parent makes no sense. After reading through that, I dove into the code and re-read the docs, carefully.
Also, if you’re reeeeaaally careful when reading the docs, it says this is possible, by not saying it.
Quoting:
Of course, the above piece of documentation does explicitly state that a map would be generated if none was provided. Though it contradicts the first thing to note, which is that the
@DiscriminatorMap
must be provided on the topmost class in the hierarchy.So, if you were to stretch your classes across several modules (as I assume that’s why you would be reading this), do not declare a discriminator map!
I’ll leave you with an example below:
When you use the doctrine CLI command to check your entities, you’ll find this is correct.
Also, check that it fully works by using the entity checking command:
Near the top of the response of that command will be this line: