2

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

Wasif Ali
  • 886
  • 1
  • 13
  • 29
Luka Žitnik
  • 1,160
  • 8
  • 15
  • paste your annotations for both classes – delboy1978uk May 25 '18 at 12:11
  • alright, I'm sure there could be multiple reasons @delboy1978uk, I'm pasting them in in a moment – Luka Žitnik May 25 '18 at 12:15
  • it turns out I would be pasting Person/SuperUser example (the single collection inheritance) form the documentation. the only difference would be an abstract class with no annotations that Person extends, but when I remove that relation I still get the same error @delboy1978uk – Luka Žitnik May 25 '18 at 12:34
  • @delboy1978uk I wasn't entirely true. I had `@String` annotation for mapping a property to the discriminator field. But when I found out I took some more time to explore. If there is any field annotation, even `@String`, the exception will be thrown. It seems like the authors wanted to stress discriminator field can only have string values, but I can't be sure because the docs only say `@String` equals to no property type mapping, no exceptions -- what a pun :) – Luka Žitnik May 25 '18 at 15:27

1 Answers1

2

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.

malarzm
  • 2,831
  • 2
  • 15
  • 25
  • 1
    I thought I should probably leave the property in the inherited class to let the code that I never looked at keep on using it like it does. But maybe I am wrong about that. I'm amazed by the effort you put into your arguments. It's great to have you around. – Luka Žitnik May 29 '18 at 14:03