How to map a union type in ManyToOne relation? #9510
-
Considering I have a property like this: class Message {
#[ORM\ManyToOne]
#[ORM\JoinColumn(name: 'receiver', referencedColumnName: 'id', nullable: false)]
private User|ChatGroup $receiver;
} If I try to make a migration for it, it fails with a
I think about two ways to solve it: just store UuidInterface of the target entities, or implement some inheritance like UserMessage and GroupMessage to make this property's type just ReflectionNamedType like this: class UserMessage {
private User $receiver;
}
class GroupMessage {
private ChatGroup $receiver;
} But both of these ways seems a little clunky to me. Is there a way to map a union type now? If not, how to solve this problem in a proper way? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Introduce a common superclass and configure a discriminator mapping there. https://www.doctrine-project.org/projects/doctrine-orm/en/2.11/reference/inheritance-mapping.html |
Beta Was this translation helpful? Give feedback.
Introduce a common superclass and configure a discriminator mapping there.
https://www.doctrine-project.org/projects/doctrine-orm/en/2.11/reference/inheritance-mapping.html