Filesystem Agnostic Uploads With Doctrine ORM

Managing file uploads for Doctrine ORM entities can take a fair amount of code to get setup. Here's a way to simplify the process with a drop-in Composer Package.

Managing file uploads for Doctrine ORM entities can take a fair amount of code to get setup. Here's a way to simplify the process with a drop-in Composer Package.

All of the heavy lifting is delegated to two excellent PHP libraries, Flysystem to handle the location abstraction and Sirius Upload to validate the files.

All you have to do is configure the entity/field combinations that you want to target. Here's a very quick example of how to implement the package.

use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Doctrine\Uploader\Listener;
use Doctrine\Uploader\Handler;

$targets = [
    'Myproject\Entity\User' => ['logo','profilepic'],
    'Myproject\Entity\Company' => ['logo']
];

$fsAdapter = new Local('/path/to/save');
$filesystem = new Filesystem($fsAdapter);
$handler = Handler($filesystem);

$em->getEventManager()->addEventListener([Doctrine\ORM\Events::preUpdate], new Listener($handler, $targets));

There's a few more refinements coming shortly but this still gives you enough power to automatically persist your file uploads to either a local filesystem or several cloud service based ones.

Here is the github repo. https://github.com/rossriley/doctrine-uploader