Search code examples
phpdatabasesymfonysymfony-formssymfony-3.2

Linking an entity to have several images and videos


I'll try to explain my problem as much as I can.

Let's say I have an entity called Profile. This entity has a few fields (name (string), description (text), birthdate (date))... etc. And I want to add to this entity images and videos, so these would be shown in the user's profile. If it was just one image and one video to upload I would've used a field image (string) and video (text) or something like that. But knowing that I want the user to upload several of each, it won't work. The images will be uploaded by the user in the form. And for the videos, he needs to paste an embeded tag from any major website.

How to do this please? I thought about creating a junction table for both an image entity and videos entity. But I'm not sure at all if that's the right way to do it either in general or in Symfony specifically.


Solution

  • You should consider the following approach:
    - create tables for photos and videos (create entities Photo and Video if you are using Doctrine);
    - add column userId to the corresponding tables (add this link property User to the corresponding entities);
    - insert/update the corresponding tables (via entities) when a user uploads/edits a new photo or video.
    Remember, you have many (in general) photos/videos for one user but only one user (owner) for each photo/video.
    If you are using Doctrine this short tutorial about associations mapping will be useful for you:
    http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html