I handled this by creating an abstract class that Entities can extend if they handle file loading, as described in the Symfony documentation. I created an array of files in order to create a copy of the existing file path in the set methods so that it can be deleted from the file system upon successful update or deletion without defining any additional properties in the Entity itself.
use Symfony\Component\HttpFoundation\File\File; abstract class FileUploadEntity { private $files; public function __set($name, File $value) { $this->files[$name] = $value; } public function __get($name) { if (!is_array($this->files)) $this->files = array(); if (!array_key_exists($name, $this->files)) { return null; } return $this->files[$name]; } public function getUploadRootDirectory() { return $this->getWebDirectory() . $this->getUploadDirectory(); } public function getWebDirectory() { return __DIR__ . "/../../../../web/"; } public function getUploadDirectory() { $year = date("Y"); $month= date("m"); return "images/uploads/$year/$month/"; } public function getEncodedFilename($name) { return sha1($name . uniqid(mt_rand(), true)); }
source share