Installation:
composer require dontdrinkandroot/doctrine-bundle
Add the bundle to config/bundles.php in a Symfony/Laravel-Symfony hybrid project:
return [
// ...
Dontdrinkandroot\DoctrineBundle\DontdrinkandrootDoctrineBundle::class => ['all' => true],
];
Configuration:
Override default settings in config/packages/dontdrinkandroot_doctrine.yaml (if using Symfony) or manually configure in Laravel's service container.
First Use Case:
Use the DoctrineExtensions service to enable behaviors like:
// Example: Enable Sluggable behavior for an entity
use Gedmo\Sluggable\SluggableListener;
$entityManager->getEventManager()->addEventSubscriber(new SluggableListener());
Entity Listeners/Subscribers:
Register Doctrine event listeners (e.g., Sluggable, Timestampable) via the bundle’s configuration:
# config/packages/dontdrinkandroot_doctrine.yaml
doctrine:
orm:
listeners:
gedmo_sluggable: true
gedmo_timestampable: true
Custom Extensions:
Extend existing behaviors (e.g., add a SoftDeleteable trait) by:
services.yaml:
services:
App\EventListener\CustomSoftDeleteListener:
tags:
- { name: 'doctrine.event_subscriber', connection: 'default' }
Laravel Integration:
Use the bundle’s services in Laravel via Symfony’s HttpKernel (if hybrid) or manually bind Doctrine components:
// config/app.php (Laravel)
'providers' => [
// ...
Dontdrinkandroot\DoctrineBundle\ServiceProvider::class,
],
Query Utilities:
Leverage the bundle’s query helpers (e.g., TreeType, Sortable) in repositories:
$qb = $entityManager->createQueryBuilder();
$qb->select('e')
->from('App\Entity\Post', 'e')
->add('orderBy', 'e.position ASC');
Outdated Dependencies:
Sluggable). Ensure compatibility:
composer require gedmo/doctrine-extensions
config/bundles.php vs. AppKernel).Laravel-Specific Issues:
symfony/doctrine-bridge or manually integrate Doctrine services.AppServiceProvider:
public function register()
{
$this->app->bind('doctrine', function ($app) {
return new \Doctrine\ORM\EntityManager($app['doctrine.connection']);
});
}
Configuration Overrides:
bootstrap/app.php (Laravel) or config/packages/doctrine.yaml (Symfony):
// Laravel: Manually register listeners
$entityManager->getEventManager()->addEventSubscriber(new \Gedmo\Sluggable\SluggableListener());
Event Subscriber Debugging:
$subscribers = $entityManager->getEventManager()->getListeners();
dump(array_keys($subscribers)); // Look for 'prePersist', 'preUpdate', etc.
.env:
DOCTRINE_ORM_DEBUG=true
Query Logging:
$entityManager->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
Custom Behaviors:
Versionable) by extending Gedmo\AbstractBehavior and register it via the bundle’s configuration.Lifecycle Callbacks:
postPersist):
// In a custom subscriber
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getObject();
if ($entity instanceof Post) {
// Send email logic
}
}
Hybrid Projects:
HttpKernel:
$kernel = new AppKernel('prod', false);
$kernel->boot();
$container = $kernel->getContainer();
$doctrine = $container->get('doctrine');
How can I help you explore Laravel packages today?