DoctrineBundle

Symfony bundle that wires Doctrine ORM and DBAL into your application

Framework
Composer
v2.19.0
4,832stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
88/100Excellent
Development Activity92
Maintenance96
Community64
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
80/100Excellent
Architecture86
Code Quality88
Innovation74
Learning Curve72

DoctrineBundle is the official integration layer between the Doctrine ORM/DBAL persistence libraries and the Symfony framework. Rather than requiring you to manually construct an EntityManager, connection factory, and metadata driver chain, the bundle registers all of it as Symfony services through a dependency-injection extension, reads database and ORM configuration from config/packages/doctrine.yaml, and exposes a set of console commands (doctrine:database:create, doctrine:schema:update, doctrine:migrations:* via companion bundles, etc.) for day-to-day database operations.

Beyond basic wiring, it adds Symfony-specific conveniences on top of vanilla Doctrine: attribute-based entity listeners and middleware registration, a ServiceEntityRepository base class that autowires the entity manager, a data collector for the Symfony profiler toolbar showing query counts and timings, cache warmers for metadata/proxy classes, and compiler passes that let other bundles hook into repository and ID-generator registration. It supports multiple connections and entity managers in a single application, making it the standard way any Symfony project talks to a relational database.

What You Get

  • A DependencyInjection extension that builds EntityManager, Connection, and metadata-driver services from doctrine.yaml configuration, supporting multiple connections/entity managers per application
  • Console commands for schema and database management (doctrine:database:create/drop, doctrine:schema:update/validate, cache clearing) registered automatically in the Symfony console
  • ServiceEntityRepository — a base repository class that autowires the correct entity manager, removing the boilerplate of manually injecting EntityManagerInterface into every repository
  • Attribute-driven configuration: #[AsEntityListener], #[AsDoctrineListener], and #[AsMiddleware] let listeners and DBAL middleware register themselves without manual service tagging
  • A Symfony profiler data collector showing per-request query counts, execution time, and duplicate-query warnings for performance debugging

Common Use Cases

  • Standard relational-database persistence in any Symfony application — entity mapping, migrations (via doctrine/doctrine-migrations-bundle), and repository queries
  • Multi-database or multi-tenant Symfony applications configuring several named connections and entity managers side by side
  • Adding custom DBAL middleware (query logging, read/write splitting via PrimaryReadReplicaConnection) without hand-registering services
  • Debugging N+1 queries and slow database calls during development using the Symfony profiler’s Doctrine panel

Under The Hood

Architecture — The bundle’s core is DependencyInjection/DoctrineExtension.php, a large compiler-time class that reads the doctrine.orm/doctrine.dbal configuration tree and registers Connection, EntityManagerInterface, and mapping-driver services (attribute, XML, or static PHP drivers) into the container; DependencyInjection/Compiler/ServiceRepositoryCompilerPass.php and IdGeneratorPass.php run as compiler passes to wire custom repositories and ID generators discovered via service tags, keeping the bulk of the integration logic at container-build time rather than request time. Tech Stack — PHP 8.4+, built directly on doctrine/dbal ^4.0, doctrine/orm (dev dependency, ^3.4 for testing), and Symfony 6.4/7.0/8.0 components (dependency-injection, config, console, cache, doctrine-bridge); static analysis is enforced with PHPStan at strict-rules level plus the phpstan-symfony and phpstan-phpunit extensions, and style with doctrine/coding-standard. Code Quality — 31 PHPUnit test classes cover the DI extension, commands, cache warmers, and mapping drivers; the codebase is organized into clear single-responsibility directories (Command/, CacheWarmer/, Mapping/, Middleware/, Repository/, Attribute/), all under declare(strict_types=1), with CI running coverage via Codecov. API Design — Configuration is entirely declarative YAML with sane defaults, so a typical Symfony project needs only a connection URL to get a working EntityManager; the attribute-based listener/middleware registration (#[AsEntityListener], #[AsMiddleware]) and the autowiring ServiceEntityRepository base class substantially cut the boilerplate compared to wiring Doctrine into a non-Symfony PHP project by hand.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search