问题描述
我试图在另一个实体中使用一个 Discriminator
。这是我做的代码:
/ **
* @ 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 correoelectrónicoyaestásiendo usado。)
* @ Gedmo\SoftDeleteable(fieldName =deletedAt,timeAware = false )
* /
class Usuario extends BaseUser {
....
}
但是,当运行命令 doctrine:schema:validate
时,我收到此错误:
任何方法来解决这个问题?扩展类可以使用Discriminator吗?
答案是在警告
消息! p>
基本上,它告诉你 Usuario
的定义方式可能会导致麻烦。在目前的形式中,此代码可让您创建一个 Usuario
的实例,并使用它。但等一下这在鉴别器图中没有定义。那么当你试图坚持下去会发生什么呢? Boom! ...或至少会抛出一个丑陋的例外。
现在,我知道你可能甚至没有想到实例化 Usuario
。这只是 Natural
和 Empresa
的基础类,但 Doctrine不知道 。
那么你怎么解决呢?根据您的需要,有两种可能的情况:
Usuario
应该是可实例化的
也就是说,您的应用程序中的用户可以是 Natural
, Empresa
或只是简单 Usuario
。这可能不是这样,但它可能适用于未来的读者。
解决方案:添加 Usuario
到鉴别器图。这将使您的用户成为这三种类型之一。
* ...
* @ ORM\DiscriminatorMap({
*usuario=Usuario,
*natural=Natural,
*empresa=Empresa
*})
* ...
Usuario
应该不是可实例化
也就是说,您的应用程序中的用户可以是
或 Empresa
,但从不 Usuario
。
解决方案:使 Usuario
一个抽象
类。这将使它无法实例化。
抽象类Usuario扩展BaseUser {
...
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
:
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 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 {
...
这篇关于在扩展另一个的实体中使用鉴别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!