bengor-file/doctrine-orm-bridge
Doctrine ORM bridge for BenGorFile/File. Provides adapters/mappings to persist File domain objects with Doctrine ORM in PHP (>=5.5). Install via Composer; fully tested with PHPSpec. Documentation lives in the main File library docs.
Install the Package
composer require bengor-file/doctrine-orm-bridge
Ensure your Laravel project uses Doctrine ORM (e.g., via doctrine/orm or a Laravel Doctrine integration package like laravel-doctrine/orm).
Register the Bridge
Add the bridge to your Doctrine ORM configuration (e.g., in config/doctrine.php or your Doctrine setup):
use BenGorFile\DoctrineORMBridge\FileType;
$types['file'] = FileType::class;
First Use Case: Storing a File Entity
Define an entity with a File field (from the BenGorFile/File library) and map it to Doctrine:
use Doctrine\ORM\Mapping as ORM;
use BenGorFile\File;
/**
* @ORM\Entity
*/
class UploadedFile
{
/**
* @ORM\Column(type="file")
*/
private $file;
public function setFile(File $file): void
{
$this->file = $file;
}
public function getFile(): ?File
{
return $this->file;
}
}
Persist a File
use BenGorFile\File;
use Doctrine\ORM\EntityManagerInterface;
$file = new File('path/to/local/file.pdf');
$upload = new UploadedFile();
$upload->setFile($file);
$entityManager->persist($upload);
$entityManager->flush();
use BenGorFile\File;
use Doctrine\ORM\EntityManagerInterface;
public function upload(Request $request, EntityManagerInterface $em)
{
$file = new File($request->file('document')->path());
$upload = new UploadedFile();
$upload->setFile($file);
$em->persist($upload);
$em->flush();
return redirect()->back()->with('success', 'File uploaded!');
}
$files = $em->createQuery('SELECT u.file FROM App\Entity\UploadedFile u')
->getResult();
find(), findBy(), and custom queries.name, size, mimeType) via Doctrine:
$uploadedFile = $em->find(UploadedFile::class, 1);
$file = $uploadedFile->getFile();
echo $file->getName(); // "document.pdf"
$uploadedFile = $em->find(UploadedFile::class, 1);
$em->remove($uploadedFile);
$em->flush();
File lifecycle callbacks (e.g., preRemove) to handle cleanup.Service Container Binding
Bind the File entity to Doctrine’s type system in a service provider:
public function boot()
{
$this->app->make('doctrine')->getConfiguration()
->addCustomStringType('file', FileType::class);
}
Form Request Validation Use Laravel’s validation with Doctrine-ORM-bridge:
public function rules()
{
return [
'document' => 'required|file|mimes:pdf,docx',
];
}
File Storage Backends
Configure the underlying BenGorFile/File library to use Laravel’s storage (e.g., S3, local):
$file = new File();
$file->setStorage(new \BenGorFile\Storage\Adapter\LocalAdapter(
storage_path('app/public')
));
Outdated Package
Missing Doctrine Events
prePersist, preRemove).$em->getEventManager()->addEventListener(
UploadedFile::class,
new FileLifecycleListener()
);
File Path Handling
File objects are already initialized with a path. Passing uninitialized files may cause errors.if (!$file->getPath()) {
throw new \RuntimeException('File path not set.');
}
Query Limitations
WHERE file.name LIKE '%pdf%') may fail if the bridge doesn’t support the underlying File methods.Doctrine Logging Enable SQL logging to debug queries:
$em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
File Object Dumping
Use var_dump() or dd() to inspect File objects during debugging:
dd($uploadedFile->getFile()->getMetadata());
Event Listener Debugging Add logging to custom listeners:
public function prePersist(LifecycleEventArgs $args)
{
\Log::debug('File prePersist triggered', [
'file' => $args->getObject()->getFile()->getPath()
]);
}
Custom File Types
Extend the bridge to support additional file types (e.g., Image):
class ImageType extends FileType
{
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value instanceof \BenGorFile\Image) {
return $value->getPath();
}
return parent::convertToDatabaseValue($value, $platform);
}
}
Storage Adapter Integration Override the default storage adapter for cloud storage:
$file->setStorage(new \BenGorFile\Storage\Adapter\S3Adapter(
new \Aws\S3\S3Client([...])
));
Doctrine Events Add custom logic to Doctrine events:
$em->getEventManager()->addEventListener(
\Doctrine\ORM\Events::preUpdate,
function ($event) {
$file = $event->getObject()->getFile();
if ($file->isDirty()) {
// Custom logic (e.g., update checksum)
}
}
);
Type Mapping
Ensure the file type is registered before entity metadata is loaded:
$config = $em->getConfiguration();
$config->addCustomStringType('file', FileType::class);
Case Sensitivity
Doctrine column types are case-sensitive. Use type="file" (lowercase) in annotations/YAML.
PHP Version The package requires PHP 5.5+, but some features (e.g., return type hints) may need PHP 7.1+ for full compatibility.
How can I help you explore Laravel packages today?