maurice2k/mezzio-doctrine

Doctrine ORM integration for Mezzio/Laminas using abstract service factories

Maintainers

Package info

github.com/maurice2k/mezzio-doctrine

pkg:composer/maurice2k/mezzio-doctrine

Statistics

Installs: 4

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.1 2026-04-16 07:56 UTC

This package is auto-updated.

Last update: 2026-04-16 10:06:56 UTC


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.