Using discriminator in a entity that extends another

前端 未结 1 807
情书的邮戳
情书的邮戳 2021-02-19 12:26

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=\"usuario         


        
相关标签:
1条回答
  • 2021-02-19 12:52

    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 {
        ...
    
    0 讨论(0)
提交回复
热议问题