Using discriminator in a entity that extends another Using discriminator in a entity that extends another symfony symfony

Using discriminator in a entity that extends another


The answer is right there in the warning message!

Basically, it's telling you that Usuario is defined in a way that could lead to trouble. In its current form, this code lets you make an instance of Usuario and work with it. But wait a second. That's not defined in the discriminator map. So, what's going to happen when you try to persist it? Boom!... or at least it will throw an ugly exception.

Now, I know you probably didn't even think about instantiating Usuario. It's just a base class for Natural and Empresa, but Doctrine doesn't know that.

So, how can you fix it? There are two possible scenarios depending on your needs:

Usuario should be instantiable

That is, users in your application can be an instance of Natural, Empresa or just plain Usuario. This is probably not the case, but it may apply to a future reader.

Solution: add Usuario to the discriminator map. This will make it possible for your users to be one of any of those three types.

 * ... * @ORM\DiscriminatorMap({ *     "usuario" = "Usuario", *     "natural" = "Natural", *     "empresa" = "Empresa" * }) * ...

Usuario should not be instantiable

That is, users in your application can either be an instance of Natural or Empresa, but never Usuario.

Solution: make Usuario an abstract class. This will make it impossible for it to be instantiated.

abstract class Usuario extends BaseUser {    ...