maurice2k / mezzio-doctrine
Doctrine ORM integration for Mezzio/Laminas using abstract service factories
Requires
- php: ^8.4
- doctrine/dbal: ^4.0
- doctrine/orm: ^3.6
- laminas/laminas-servicemanager: ^3.22 || ^4.0
- laminas/laminas-stdlib: ^3.20
- psr/container: ^1.1 || ^2.0
Requires (Dev)
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^13.0
- symfony/var-exporter: ^7.0
README
Doctrine ORM integration for Mezzio (formerly Zend Expressive) using a Laminas-compatible abstract service factory.
This package provides a lightweight, convention-based approach to wire up Doctrine ORM services (EntityManager, Connection, Configuration, Driver, EventManager) through PSR-11 containers with a single ConfigProvider.
Installation
composer require maurice2k/mezzio-doctrine
If you're using Laminas component installer, the ConfigProvider is registered automatically. Otherwise, add it to your config/config.php:
$aggregator = new ConfigAggregator([ \Maurice2k\MezzioDoctrine\ConfigProvider::class, // ... ]);
Configuration
Add a doctrine key to your application config (e.g. config/autoload/doctrine.global.php):
return [ 'doctrine' => [ 'connection' => [ 'orm_default' => [ 'params' => [ 'driverClass' => \Doctrine\DBAL\Driver\PDO\MySQL\Driver::class, 'host' => 'localhost', 'port' => '3306', 'user' => 'root', 'password' => '', 'dbname' => 'mydb', ], ], ], 'driver' => [ 'orm_default' => [ 'class' => \Doctrine\Persistence\Mapping\Driver\MappingDriverChain::class, 'drivers' => [ 'App\\Entity' => [ 'class' => \Doctrine\ORM\Mapping\Driver\AttributeDriver::class, 'paths' => [__DIR__ . '/../../src/App/Entity'], ], ], ], ], 'configuration' => [ 'orm_default' => [ 'proxy_dir' => 'data/cache/DoctrineORMProxy', 'proxy_namespace' => 'DoctrineORMProxy', ], ], 'event_manager' => [ 'orm_default' => [ 'subscribers' => [], 'listeners' => [], ], ], ], ];
How it works
The package registers a Laminas AbstractFactoryInterface (DoctrineServiceFactory) that intercepts service requests matching the pattern doctrine.<category>.<key> and delegates to the appropriate factory:
| Category | Factory | Produces |
|---|---|---|
configuration |
ConfigurationFactory |
Doctrine\ORM\Configuration |
connection |
ConnectionFactory |
Doctrine\DBAL\Connection |
driver |
DriverFactory |
MappingDriver |
entity_manager |
EntityManagerFactory |
Doctrine\ORM\EntityManager |
event_manager |
EventManagerFactory |
Doctrine\Common\EventManager |
Default aliases are registered so you can inject EntityManager::class, EntityManagerInterface::class, or Connection::class directly.
Multiple connections
Use different keys to set up multiple connections:
'doctrine' => [ 'connection' => [ 'orm_default' => [ /* ... */ ], 'orm_reporting' => [ /* ... */ ], ], // same for driver, configuration, entity_manager, event_manager ],
Then retrieve via $container->get('doctrine.entity_manager.orm_reporting').
Requirements
- PHP 8.4+
- Doctrine ORM 3.6+
- Doctrine DBAL 4.x
- Laminas ServiceManager 3.22+ or 4.x
License
MIT License. See LICENSE for details.