brainx2/file-fileentitybundle
Installation Add the package via Composer:
composer require brainx2/file-fileentitybundle:1.0.0.*@dev
Register the bundle in app/AppKernel.php:
new Brainx2\File\FileEntityBundle\Brainx2FileFileEntityBundle(),
Entity Setup
Extend your entity from Brainx2\File\FileEntityBundle\Entity\FileEntity and define file fields (e.g., image_first, image_second) as string types in your YAML/ORM mapping.
First Use Case
Upload a file by setting the field value to a file path or UploadedFile object:
$demo = new Demo();
$demo->setName('Test Demo');
$demo->setImageFirst($request->files->get('image_first')); // Symfony UploadedFile
$entityManager->persist($demo);
$entityManager->flush();
The bundle automatically handles file uploads via lifecycle callbacks (prePersist, preUpdate).
File Upload Handling
UploadedFile or file paths for file fields.$entity->setImageFirst($request->files->get('image'));
$entityManager->flush(); // Triggers upload lifecycle
Custom File Paths
Override getUploadRootDir() in your entity to specify a custom upload directory:
public function getUploadRootDir()
{
return __DIR__ . '/../../../../web/uploads/custom';
}
File Removal
The preRemove lifecycle callback deletes the file when the entity is removed. Ensure the file path is valid before deletion.
Validation
Add validation constraints (e.g., File, MimeType) to file fields in your entity:
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\File(
* maxSize="1024k",
* mimeTypes={"image/jpeg", "image/png"}
* )
*/
protected $image_first;
Integration with Forms
Use Symfony’s FileType field in forms:
$builder->add('image_first', FileType::class, [
'label' => 'Upload Image',
'mapped' => false, // Required for UploadedFile handling
]);
preUpload or postUpload events (if the bundle exposes them) for custom logic.prePersist/preUpdate hooks in your entity for additional processing.File Path Handling
getUploadRootDir() returns an absolute path or the bundle may fail silently.__DIR__ or Symfony’s ParameterBag for flexibility.Lifecycle Callback Conflicts
upload or remove callbacks are overridden elsewhere, the bundle’s logic may not execute. Use parent::method() to chain behavior.File Permissions
chmod 775) after installation.UploadedFile vs. Paths
UploadedFile objects for new uploads. For existing files, use paths (e.g., ./uploads/image.jpg).Entity Inheritance
FileEntity, ensure all file fields are marked as protected or private to trigger the bundle’s magic methods.getUploadRootDir() and verify the directory exists.APP_DEBUG=true) to catch lifecycle callback errors.$errors = $validator->validate($entity);
foreach ($errors as $error) { dump($error->getMessage()); }
Custom File Naming
Override generateUniqueFilename() in your entity to control file names:
protected function generateUniqueFilename($field, $originalFilename)
{
return 'custom_' . uniqid() . '_' . pathinfo($originalFilename, PATHINFO_FILENAME);
}
Multiple File Fields
The bundle supports multiple file fields (e.g., image_first, image_second). Each must be defined in the ORM and entity.
Testing
Mock UploadedFile in tests:
$uploadedFile = $this->createMock(UploadedFile::class);
$uploadedFile->method('getClientOriginalName')->willReturn('test.jpg');
$entity->setImageFirst($uploadedFile);
Configuration
No bundle-specific configuration is required, but you can extend the bundle’s services (e.g., file_entity.listener) for custom logic.
Performance For large files, consider streaming uploads or async processing to avoid timeouts. The bundle does not handle this natively.
How can I help you explore Laravel packages today?