Closed bastien70 closed 9 months ago
So is this the same issue as #1172 or a solution for it? Or is it a new issue?
It's basically the same problem, but brought up to date, so that it appears in recent issues.
And a possible solution
You can bring it up by commenting on it. Closing as a duplicate
Bug Report
Summary
There is a problem that has not been resolved for several years.
When we use a generic entity (for example, called "Media"), and we use it as a relationship for all our entities requiring the use of files, then even if we implement a delete cascade on the relationship, if we delete the file, the record of the generic entity is not deleted (or rather, it is recreated directly with the creation and edition dates, without file, and is again associated with the entity)
See https://github.com/dustin10/VichUploaderBundle/issues/1172
How to reproduce
And I associate it with my entities. By example, in my User entity, I've an avatar field :
In the vich_uploader.yaml :
When clicking inside the checkbox and submitting the form to remove current avatar relation, the file is deleted, the Media entity is truncated, but it creates a new media (with just createdAt and updatedAt values) instead of just deleting by the way the "media" row in database
UPDATE :
I found a solution to make it work. In my entities having a relationship with Media, I add the ORM\JoinColumn(onDelete: 'SET NULL')
And I set up an event when a Media entity is modified which checks that we still have a filename. If not, I delete the Media.