-1

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 video
  • author_id: is the user_id who authored the entity
  • entity: is a constant like document or video 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 "DocumentorVideo`" 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?

2 Answers 2

0

Don't know if would be better to store it under two differents attributes. If not and mandatory, I think those "objects" should have a common interface or something, so take a look to doctrine inheritance that should fulfill your needs

1
0

My suggestion is to store the entity namespace Ex. Acme\Entity\Document in a property and the id in another and to use the entity manager to get the entity.

Edit: Though you won't have the relation, I prefer that way over others because it is reusable and the performance is rather the same. Also if I need to pass it to a JSON response, I just create a normalizer and I am good to go.

/**
 * @ORM\Column(type="string")
 */
private $entityNamespace;

/**
 * @ORM\Column(type="integer")
 */
private $entityId;

public function __construct(EntityManagerInterface $em)
{
    $this->em = $em;
}

public function getEntity()
{
    return $this->em->getRepository($this->entityNamespace)->find($this->entityId);
}
0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.