adcog-cpi/doctrine-bundle
Doctrine bundle providing reusable entity tools: automatic created/updated timestamps, slug and salt generation, filesystem storage paths, loggable messages, and paginator defaults. Includes a command to fix/rebuild slugs across the database.
Installation
composer require adcog-cpi/doctrine-bundle
Ensure EBDoctrineBundle is enabled in config/bundles.php:
return [
// ...
AdcogCpi\DoctrineBundle\EBDoctrineBundle::class => ['all' => true],
];
Basic Configuration Publish the default config to customize:
php bin/console config:dump-reference AdcogCpi\DoctrineBundle\Configuration
Override in config/packages/eb_doctrine.yaml:
eb_doctrine:
filesystem:
web_path: "/uploads"
secured_path: "%kernel.project_dir%/var/uploads"
First Use Case: File Handling
Use the FileManager service to handle file uploads/storing:
use AdcogCpi\DoctrineBundle\Service\FileManager;
class MyController extends AbstractController {
public function upload(FileManager $fileManager, UploadedFile $file) {
$path = $fileManager->store($file, 'user_avatars');
// $path now contains the secured path (e.g., /var/uploads/user_avatars/...)
}
}
Storing Files
$fileManager->store($uploadedFile, 'entity_class_name', $entityId);
web_path/secured_path + use_env_discriminator/use_class_discriminator./var/uploads/dev/User/123/filename.ext (if use_env_discriminator and use_class_discriminator are true).Retrieving Files
$webUrl = $fileManager->getWebPath($securedPath);
// Converts secured path to web-accessible URL (e.g., /uploads/dev/User/123/filename.ext)
Deleting Files
$fileManager->delete($securedPath);
Use the Paginator service for consistent pagination across controllers:
use AdcogCpi\DoctrineBundle\Service\Paginator;
class ProductController {
public function index(Paginator $paginator, EntityManagerInterface $em) {
$query = $em->createQuery('SELECT p FROM App\Entity\Product p');
$results = $paginator->paginate($query, $this->get('request_stack')->getCurrentRequest()->query->getInt('page', 1));
// $results includes data + pagination metadata (total, limit, etc.)
}
}
Leverage the Logger service for standardized CRUD messages:
use AdcogCpi\DoctrineBundle\Service\Logger;
class ProductController {
public function create(Logger $logger, Product $product) {
$logger->persisted($product, 'product');
// Logs: "L'élément product a été créé avec succès !"
}
}
Path Generation Quirks
use_env_discriminator is false, paths ignore the environment (e.g., dev/prod).use_class_discriminator is false, paths flatten to /var/uploads/123/filename.ext instead of /var/uploads/User/123/filename.ext.$fileManager->getSecuredPath('test', 'User', 123);
Paginator Defaults
default_limit is null, the bundle uses Symfony’s default (e.g., 30).default_limit: 10 in config to avoid surprises.File Deletion Race Conditions
delete() method does not check if the file exists.file_exists() first:
if (file_exists($securedPath)) {
$fileManager->delete($securedPath);
}
eb_doctrine:
filesystem:
debug: true
php bin/console debug:config eb_doctrine
Custom File Naming
Override the FileNamer service to implement custom logic (e.g., UUIDs):
services:
AdcogCpi\DoctrineBundle\Service\FileNamer:
class: App\Service\CustomFileNamer
arguments:
$original: '@AdcogCpi\DoctrineBundle\Service\FileNamer'
Translation Keys
Replace hardcoded messages with translation keys by extending the Logger:
$logger->setTranslationDomain('messages');
$logger->persisted($entity, 'product'); // Uses 'messages.product.created' key
Depth Handling
depth: 3 to limit path depth (e.g., /var/uploads/User/12/3/filename.ext).0 disables directory structure entirely (files dumped in root).How can I help you explore Laravel packages today?