dustin10 / VichUploaderBundle

A simple Symfony bundle to ease file uploads with ORM entities and ODM documents.
MIT License
1.83k stars 519 forks source link

Problem with delete when using multiple mappings on one entity #1437

Closed barberob closed 3 months ago

barberob commented 6 months ago
Q A
Bundle version 2.3.1
Symfony version 6.4
PHP version 8.2.15

Support Question

Hey ! So i have this entity which can be related to two different other entities

class Photo
{
    // .......

    #[ORM\Column(type: 'string', length: 255)]
    private ?string $file;

    #[Vich\UploadableField(mapping: 'signalement_photo', fileNameProperty: 'file')]
    private ?File $uploadFileSignalement = null;

    #[Vich\UploadableField(mapping: 'intervention_photo', fileNameProperty: 'file')]
    private ?File $uploadFileDemandeIntervention = null;

    // .......

    #[ORM\ManyToOne(targetEntity: Signalement::class, inversedBy: 'photos')]
    #[ORM\JoinColumn(name: 'signalement_id', referencedColumnName: 'id')]
    private ?Signalement $signalement = null;

    #[ORM\ManyToOne(targetEntity: DemandeIntervention::class, inversedBy: 'photos')]
    private $demandeIntervention;

It have two different mappings because i need to store it a two different locations depending on the entity it is related to (my config)

    mappings:
        signalement_photo:
            uri_prefix: '%app.path.photoSignalement%'
            upload_destination: photo.storage.signalement
            directory_namer:
                service: vich_uploader.namer_directory_property
                options: { property: 'signalement.id' }
            namer:
                service: Vich\UploaderBundle\Naming\SmartUniqueNamer

        intervention_photo:
            uri_prefix: '%app.path.photoIntervention%'
            upload_destination: photo.storage.intervention
            directory_namer:
                service: vich_uploader.namer_directory_property
                options: { property: 'demandeIntervention.id' }
            namer:
                service: Vich\UploaderBundle\Naming\SmartUniqueNamer

Depending on some logic, i put my uploaded file on the needed UploadableField property, and the bundle handles the job, which is working really great for now. BUT when i try to delete a photo entity linked to demandeIntervention entity, i have the following error PropertyAccessor requires a graph of objects or arrays to operate on, but it found type "NULL" while trying to traverse path "signalement.id" at property "id". same thing on the other side when linked to the other entity. The bundle seems to try to delete the file on both storage, whilst it don't exist in one of the two. How can i prevent this ? PS: i need this directory structure

barberob commented 3 months ago

In case someone needs it, I solved it with a custom directory namer