Zf2 + Doctrine 2 - Child-Level Discriminators with Class Table Inheritance

ZF2 + Doctrine 2 - Child-level discriminators with Class Table Inheritance

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”}

Hibernate DiscriminatorValue equivalent in Doctrine 2

Like my comment stated, I had this issue a while back as well, where I wanted to "declare" new DiscriminatorMap entries on the child-classes. The short answer is: do not declare a map at all. Doctrine takes care of it.

Have a read of my full answer. This works for me using Class Table Inheritance (CTI), the docs state that it should work the same for Single Table Inheritance (STI).

The basic code setup to let Doctrine handle it for you is:

<?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
{
// ...
}

Symonfy2, Doctrine: duplicate, default, or ignore DiscriminatorMap?

Found an answer to one option: Ignoring

In Doctrine 2.2.x, my statements were in the JOIN, not WHERE clause. Using the example in Leave out discriminator part of Doctrine' generated SQL, I had to overload the ->walkJoin() method to get ignore to work properly:

/**
* {@inheritdoc}
*/
public function walkJoin($join)
{
$this->checkForHint();
return parent::walkJoin($join);
}

protected function checkForHint()
{
$ignoreDescription = $this->getQuery()->getHint(self::IGNORE_DISCRIMINATION);

if ($ignoreDescription) {
foreach ($this->getQueryComponents() as $k => $component) {
if (in_array($k, $ignoreDescription)) {
/** @var $meta ClassMetadata */
$meta = $component['metadata'];
$meta->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_NONE);
}
}
}
}

Still have not resolved Duplicate or Default discriminator values...



Related Topics



Leave a reply



Submit