ashleydawson/doctrine-flysystem-bundle
Install the Bundle:
composer require ashleydawson/doctrine-flysystem-bundle:0.8.*
Register in AppKernel.php:
new Oneup\FlysystemBundle\OneupFlysystemBundle(),
new AshleyDawson\DoctrineFlysystemBundle\AshleyDawsonDoctrineFlysystemBundle(),
Configure Flysystem (config.yml):
oneup_flysystem:
adapters:
local:
local:
directory: %kernel.project_dir%/var/uploads
filesystems:
uploads:
adapter: local
mount: uploads
Apply Trait to Entity:
use AshleyDawson\DoctrineFlysystemBundle\ORM\StorableTrait;
class Product {
use StorableTrait;
public function getFilesystemMountPrefix() {
return 'uploads';
}
}
Update Schema:
php bin/console doctrine:schema:update --force
Create a form to upload a file:
$builder->add('file', 'file', ['required' => false]);
Submit the form, and the bundle automatically:
fileName, fileStoragePath, fileMimeType, fileSize) in the DB.File Upload Handling:
setUploadedFile() method in controllers or form types.$product->setUploadedFile($request->files->get('file'));
$em->persist($product);
$em->flush();
Accessing File Data:
getFileName(), getFileStoragePath(), etc.).url() method:
$filesystem = $this->get('oneup_flysystem.filesystem.uploads');
$url = $filesystem->url($product->getFileStoragePath());
Multi-Filesystem Support:
getFilesystemMountPrefix() to duplicate files across adapters:
public function getFilesystemMountPrefix() {
return ['uploads', 'backups'];
}
Event-Driven Customization:
ashleydawson.doctrine_flysystem_bundle.pre_store) to modify file paths or metadata:
$dispatcher->addListener(StorageEvents::PRE_STORE, function (StoreEvent $event) {
$event->setFileStoragePath('custom/' . $event->getFileStoragePath());
});
file field type and map it to uploaded_file (or a custom setter).fileSize or fileMimeType in your entity.prePersist/preUpdate to trigger file operations manually if needed.Schema Mismatch:
doctrine:schema:update after adding the trait will cause errors.File Deletion:
delete_old_file_on_update: true). Disable this in config.yml if needed:
ashley_dawson_doctrine_flysystem:
delete_old_file_on_update: false
Event Order:
PRE_STORE fire before the file is written. Modify paths/metadata before this event if you need to influence storage.Multi-Filesystem Collisions:
PRE_STORE.$event->getFileStoragePath() in PRE_STORE to verify paths are correct.mount names in getFilesystemMountPrefix() match those in oneup_flysystem.filesystems.local) require write permissions on the target directory.Custom Field Mapping:
StorableFieldMapperInterface to change column names/types (e.g., for fileSize to use bigint).services.yml:
ashley_dawson.doctrine_flysystem.storable_field_mapper.class: AppBundle\ORM\Flysystem\CustomMapper
Pre/Post-Processing:
Doctrine Events:
onFlush to batch file operations for performance:
$dispatcher->addListener(Doctrine\ORM\Events::onFlush, function () {
// Custom logic for bulk file handling
});
isDeleted flag to entities and handle file deletion in PRE_DELETE events.aws, rackspace, or s3 adapters for remote storage (configured in oneup_flysystem).$adapter = $this->createMock(\League\Flysystem\AdapterInterface);
$filesystem = new \League\Flysystem\Filesystem($adapter);
$this->get('oneup_flysystem.filesystem.uploads')->setAdapter($adapter);
How can I help you explore Laravel packages today?