Using discriminator in a entity that extends another

感情迁移 提交于 2020-01-01 08:05:17

问题


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?


回答1:


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 {
    ...


来源:https://stackoverflow.com/questions/26049290/using-discriminator-in-a-entity-that-extends-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!