I'm trying to use a Discriminator
in a entity that extends from another. This is the code I made:
/**
* @ORM\Entity
* @ORM\Table(name="usuarios_externos.usuarios", schema="usuarios_externos")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({
* "natural" = "Natural",
* "empresa" = "Empresa"
* })
* @UniqueEntity(fields={"correo_alternativo"}, message="El correo electrónico ya está siendo usado.")
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
*/
class Usuario extends BaseUser {
....
}
But I'm getting this error while ran the command doctrine:schema:validate
:
[Doctrine\ORM\Mapping\MappingException] Entity 'UsuarioBundle\Entity\Usuario' has to be part of the discriminator map of 'UsuarioBundle\Entity\Usuario' to be properly mapped in the inheritance hierarchy. Alternatively you can make 'UsuarioBundle\Entity\Usuario' an abstract class to avoid this exception from occurring.
Any way to fix this? It's possible to use Discriminator in extended classes?
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 instantiableThat 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 instantiableThat 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 {
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With