symfonyoneupuploaderbundle

Removing orphaned entities following 'clear-orphans' command


I'm using the OneUp Uploader Bundle, with the orphanage, and I'm unsure about how to handle cleaning up entities (that were created by the listener), after the file has been cleaned up by the 'clear-orphans' command - how is this commonly handled?

I'd love to see an event fired for each file that is cleaned up (passing the filename and the mapping) but haven't found anything (assuming the event dispatcher is available to a command).


Solution

  • The idea of the orphanage in the OneupUploaderBundle is that you don't pollute your upload folder with files that don't belong to any entities. This implies that if you use the uploaded files in your entities you should move them out of the orphanage. If you configured a mapping to use the orphanage, all the uploaded files will be stored in a separate folder after uploading.

    Citing the documentation:

    They will be moved to the correct place as soon as you trigger the uploadFiles method on the Storage.

    This means that you can move the logic out of the event listener (which will be called non the less), but move it to the controller where you want to finally upload and store the files in the orphanage.

    // src/Acme/Controller/AcmeController.php
    
    class AcmeController extends Controller
    {
        public function storeAction()
        {
    
            // ...
            if ($form->isValid()) {
                $orphanageManager = $this->get('oneup_uploader.orphanage_manager')->get('gallery');
    
                // upload all files to the configured storage
                $files = $manager->uploadFiles();
    
                // your own logic to apply the files to the entity
                // ...
            }
    
        }
    }
    

    Be sure to only use the orphanage if you really have to. It has some drawbacks.