ZF2 + Doctrine 2 - Child-level discriminators with

2020-01-30 02:13发布

问题:

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.

回答1:

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:

Things to note:

The @InheritanceType, @DiscriminatorColumn and @DiscriminatorMap must be specified on the topmost class that is part of the mapped entity hierarchy.

The @DiscriminatorMap specifies which values of the discriminator column identify a row as being of which type. In the case above a value of “person” identifies a row as being of type Person and “employee” identifies a row as being of type Employee.

The names of the classes in the discriminator map do not need to be fully qualified if the classes are contained in the same namespace as the entity class on which the discriminator map is applied.

If no discriminator map is provided, then the map is generated automatically. The automatically generated discriminator map contains the lowercase short name of each class as key.

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:

<?php
namespace My\Namespace\Entity;

/**
 * @Entity
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="discr", type="string")
 * // NOTE: No @DiscriminatorMap!!!
 */
class Person
{
    // ...
}


<?php
namespace My\Other\Namespace\Entity;

/** @Entity */
class Employee extends \My\Namespace\Entity\Person
{
    // ...
}

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:

./vendor/bin/doctrine-module orm:mapping:describe “\My\Namespace\Entity\Person”

Near the top of the response of that command will be this line:

| Discriminator map | {“person”:”My\\Namespace\\Entity\\Person”,”employee”:”My\\Other\\Namespace\\Entity\\Employee”}