phpsymfonydoctrine-ormdiscriminator

Discriminator in a Joined Table with Doctrine2


I have an abstract parent class called Divers which is extended by few other classes.
So, I use inheritance mapping with D2 using Single Table Inheritance strategy.

namespace MyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * ParentClass
 * 
 * @ORM\Table(name="PARENTCLASS")
 * @ORM\Entity
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="idtable", type="string")
 * @ORM\DiscriminatorMap({
 *      "CHILD-CLASS1" = "ChildClassOne",
 *      "CHILD-CLASS2" = "ChildClassTwo",
 *      "CHILD-CLASS3" = "ChildClassThree",
 *      "CHILD-CLASS4" = "ChildClassFour"
 * })
 */
abstract class ParentClass
{
    ...
}

What I want to achieve is to display the discriminator in the browser with a little description that explains what is it to the user.
I googled for a solution like putting the discriminator in a joined table but found nothing.

Do you have any advice to achieve my goal ?

Thanks by advance for your help.


Solution

  • The discriminator column has special meaning for Doctrine 2 and thus cannot be part of a relation.

    But there is an easy work around. Just add another column and give it the same value that your discriminator column has. The value will never change so it's easy enough to do. You can then of course use your new column in the same way as any other column.

    I know having two columns with the same value is not ideal from a database perspective. But from an object perspective, it's no big deal since the discriminator column is never exposed as a property. And it's just the way doctrine works. It wants that column all to itself.