symfony doctrine pivot table with multiple keys

Viewed 773

Imagine the following usecase describing it just schemantically, but i belive it is enough:

1-have some kind of entities, lets call the company and person:

company (id=uuid, name=string, files=hasMany(File), ... )
person (id=uuid, name=string, files=hasMany(File), ... )

2-have a file entity like :

file (id=uuid, fileName=string, filePath=string, ...)

at some point i would like to store File for Company or Person or any other entity, in a pivot table (one single pivot table, not a different one for each EntityType-File relation).

this would mean having a pivot table like

fileId | entityId | entityType
1         2          CLIENT
2         2          PERSON
.....

and so on. note the difference in the entityType: entityId is the same (2) but the type is different, bsicly this translates to "fileId(2) belongs to the id 2 of the Person entity).

how could i map this in the entity using doctrine and annotations? the basic problem is to introduce the entityType column and set it for each entity to a constant (it could be a string like CLIENT or an int (like 1 = client, 2 = person ...)

this way the only one File entity could be reused on any number of entities of any kind, where file(s) upload is needed.

can someone point me in a right direction?

0 Answers
Related