Search code examples
phpmongodbdoctrinedoctrine-odm

What is discriminator field conflict in PHP Doctrine MongoDB ODM?


What does it mean when error

Discriminator field "x" in "y" conflicts with a mapped field's "name" attribute.

is thrown?

More specifically I mean this condition:

    if ($this->discriminatorField !== null && $this->discriminatorField === $mapping['name']) {
        throw MappingException::discriminatorFieldConflict($this->name, $this->discriminatorField);
    }

Referal Code


Solution

  • Discriminator fields are not supposed to be mapped to properties thus the exception you're seeing. In theory, developers should not be interested in discriminator field's value as it's useful for the ODM to decide what object should be instantiated or how to query for documents in case of single collection inheritance. In userland all you should worry about is the type of class you're operating on.

    Now for some history, the exception was introduced in BETA-10 in 2013 but before it was also impossible to map a discriminator to a property, the commit seems to harden the guard and introduce an exception that is less confusing. Given previous changes made 6 years ago now I'd say it was never possible to get your hands on discriminator fields through mapped properties.