dustin10 / VichUploaderBundle

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

vich_uploader.pre_remove event is not triggered #1268

Closed sdespont closed 2 years ago

sdespont commented 2 years ago
Q A
Bundle version 1.19.0
Symfony version 4.4.37
PHP version 7.4.26

Support Question

I can upload a file and update it. But when I update the file, I want to remove the file by myself, and that part is not working.

After playing with the bundle during hours, I can't figure out why the vich_uploader.pre_remove event is not triggered when I am doing an entity update. Only the vich_uploader.pre_upload and vich_uploader.post_upload are triggered.

But the delete_on_update option is set to true, do the vich_uploader.pre_remove should be triggered as well.

image

Here is my config:

App\Entity\Poll\Poll:
    # File entity name
    scanned_document:
        # Vich mapping name in config.yml
        mapping:           poll_document
        # Entity field name in ORM
        filename_property: scanned_document_path
vich_uploader:
    db_driver: orm
    metadata:
        auto_detection: false
        directories:
            - { path: '%kernel.project_dir%/config/vich_uploader', namespace_prefix: 'App' }
    mappings:
        poll_document:
            uri_prefix:         "/%poll_document_folder%"
            upload_destination: "%kernel.project_dir%/%poll_document_folder%"
            namer:              App\Utils\UploadFile\PropertyFileNamer
            directory_namer:    App\Utils\UploadFile\PropertyDirectoryNamer
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true
App\EventListener\ToolBox\UploadFileListener:
    class: App\EventListener\ToolBox\UploadFileListener
    arguments:
        $documentProjectDir: "%kernel.project_dir%/%data_documents_folder%"
    tags:
        - { name: kernel.event_listener, event: vich_uploader.pre_remove, method: onVichUploaderPreRemove }
        - { name: kernel.event_listener, event: vich_uploader.post_remove, method: onVichUploaderPostRemove }
        - { name: kernel.event_listener, event: vich_uploader.pre_upload, method: onVichUploaderPreUpload }
        - { name: kernel.event_listener, event: vich_uploader.post_upload, method: onVichUploaderPostUpload }

In the entity

// *************************************************************************
// Upload file
// *************************************************************************

protected ?File $scanned_document = null;

/**
 * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
 * of 'UploadedFile' is injected into this setter to trigger the  update. If this
 * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
 * must be able to accept an instance of 'File' as the bundle will inject one here
 * during Doctrine hydration.
 *
 * @param File|UploadedFile $scannedDocument
 */
public function setScannedDocument(File $scannedDocument) : void
{
    $this->scanned_document = $scannedDocument;

    if ($scannedDocument) {
        // It is required that at least one field changes if you are using doctrine
        // otherwise the event listeners won't be called and the file is lost
        $this->updated = new DateTime('now');
    }
}

/**
 * @return File|null
 */
public function getScannedDocument() : ?File
{
    return $this->scanned_document;
}

I would really appreciate some help because I am lost...

garak commented 2 years ago

See #1252