daviddel/doctrine-manager
Laravel package to manage Doctrine ORM/DBAL inside your app. Provides a manager/service integration for setting up and accessing Doctrine connections and entity managers through Laravel’s container and config, aiming to simplify Doctrine usage alongside Eloquent.
Installation:
composer require daviddel/doctrine-manager
Add to config/app.php under providers:
Daviddel\DoctrineManager\DoctrineManagerServiceProvider::class,
Publish Config (if needed):
php artisan vendor:publish --provider="Daviddel\DoctrineManager\DoctrineManagerServiceProvider"
Config file: config/doctrine-manager.php.
Basic Usage: Register a Doctrine entity manager in Laravel's IoC container:
// In a service provider (e.g., AppServiceProvider)
$this->app->singleton('doctrine.manager', function ($app) {
return DoctrineManager::getManager();
});
First Use Case: Fetch an entity manager in a controller or service:
use Daviddel\DoctrineManager\Facades\DoctrineManager;
public function index()
{
$em = DoctrineManager::getManager();
$users = $em->getRepository('App\Entity\User')->findAll();
return view('users.index', compact('users'));
}
Entity Management:
$user = $em->getRepository('App\Entity\User')->find(1);
$em->persist($newUser);
$em->flush();
$query = $em->createQuery('SELECT u FROM App\Entity\User u WHERE u.active = :active')
->setParameter('active', true);
$activeUsers = $query->getResult();
Integration with Laravel:
public function __construct(DoctrineManager $manager) {
$this->manager = $manager;
}
Doctrine\ORM\EntityRepository and bind them:
$this->app->bind('App\Repositories\UserRepository', function ($app) {
return $app['doctrine.manager']->getRepository('App\Entity\User');
});
Query Building:
$query = $em->createQuery('UPDATE App\Entity\User u SET u.lastLogin = :now WHERE u.id = :id');
$query->execute(['now' => new \DateTime(), 'id' => 1]);
Transactions: Wrap operations in transactions:
$em->beginTransaction();
try {
$em->persist($entity);
$em->flush();
$em->commit();
} catch (\Exception $e) {
$em->rollback();
throw $e;
}
Outdated Package:
doctrine/orm, doctrine/common) to match your project.Configuration Overrides:
config/doctrine-manager.php. If using Laravel's built-in Doctrine integration (e.g., laravel-doctrine/orm), conflicts may arise.Entity Namespace:
App\Entity\User) match your actual namespace. Misconfigurations lead to ClassNotFoundException.DoctrineManager::getManager()->getConfiguration()->getEntityNamespace() to verify.Singleton Manager:
$customManager = DoctrineManager::createManager(['driver' => 'your_config']);
Connection Issues:
config/doctrine-manager.php for correct connection and driver settings.$em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
Lazy-Loading:
php artisan doctrine:cache:clear-metadata
php artisan doctrine:cache:clear-query
Custom Event Listeners: Add listeners via config:
'listeners' => [
'App\Events\UserListener',
],
Or programmatically:
$em->getEventManager()->addEventListener([/* ... */]);
Custom Drivers:
Extend the package to support non-Doctrine databases (e.g., MongoDB) by implementing Daviddel\DoctrineManager\Contracts\DriverInterface.
Laravel Events: Trigger Laravel events from Doctrine lifecycle callbacks:
$em->getEventManager()->addEventListener(
\Doctrine\ORM\Events::postPersist,
function ($event) {
event(new \App\Events\UserCreated($event->getObject()));
}
);
Batch Processing:
Use Doctrine\ORM\UnitOfWork for bulk inserts/updates:
$uow = $em->getUnitOfWork();
foreach ($users as $user) {
$uow->computeChangeSet($em->getClassMetadata('App\Entity\User'), $user);
}
$em->flush();
Query Caching:
Enable second-level cache in config/doctrine-manager.php:
'cache' => [
'driver' => 'apcu',
'entity_region' => 'default',
'query_region' => 'default',
],
How can I help you explore Laravel packages today?