I'm developing a custom content management system with Symfony 5 and Doctrine.
I'm trying to implement a relation between the entities Document
and Video
(actually there are many more, but for simplicity sake let's say are just two) and the User
entity.
The relation represent the User
who wrote the document or recorded the video. So the relation here is called Author
. Each document or video can have one or more author. Each User can have none or more document or video.
I would like to use just a single associative Author
associative entity, like this:
entity_id|author_id|entity
Where:
entity_id
: is the id of the document or videoauthor_id
: is theuser_id
who authored the entityentity
: is a constant likedocument
orvideo
to know to which entity the relation refer to
The problem is that I cannot understand how to build this in Doctrine. Was this a classic SingleEntity<-->Author<-->Users
relationship I would have build it as a ManyToMany item, but here it's different.
Author
would probably contain two ManyToOne relations (one with the User
entity and one with either the Document
or the Video
entity) plus the entity type
field, but I really don't know how to code the "Documentor
Video`" part. I mean:
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity=??????????, inversedBy="authors")
* @ORM\JoinColumn(nullable=false)
*/
private $entity; // Document or Video
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="articles")
* @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @ORM\Column(type="smallint")
*/
private $entityType;
How should I manage the first field?