Overview
Doctrine ORM has been developed since 2010 in its current repository and is the object-relational mapper behind most Symfony applications. Entities are ordinary PHP classes mapped with attributes or XML; an EntityManager loads them, tracks changes in a unit of work, and writes everything on flush(). Queries are written in DQL, an object-oriented SQL dialect inspired by Hibernate's HQL, through a QueryBuilder, or in native SQL.
The ORM sits on the Doctrine DBAL, which provides platforms for MySQL, MariaDB, Oracle, SQL Server, PostgreSQL, Db2 and SQLite. Schema versioning is handled by Doctrine Migrations, a separate package for versioning the schema and deploying changes; Symfony integrates both through DoctrineBundle and DoctrineMigrationsBundle.
The project maintains several lines at once: 3.7 and 2.20 are the stable series on the project page, with 3.8, 2.21 and 4.0 listed as upcoming. The 2.x line is still maintained for applications that have not moved to 3.x.
Pricing and plans
| Plan | Price | What you get |
|---|---|---|
| Doctrine ORM | $0 | MIT licence |
What it does
- Unit of Work
- Changes to managed entities are computed and written in one flush, in dependency order.
- DQL and QueryBuilder
- Object-oriented query language plus a fluent builder; native SQL available with result mapping.
- Attribute mapping
- Map entities, associations, embeddables and inheritance with PHP attributes or XML.
- DBAL
- Database abstraction with platform classes for MySQL, MariaDB, PostgreSQL, SQLite, SQL Server, Oracle and Db2.
- Doctrine Migrations
- Separate package that versions the database schema and deploys changes.
Strengths and limitations
Strengths
- Plain PHP entities keep domain code free of persistence base classes
- Mature and deeply integrated with Symfony
- Supports Oracle, SQL Server and Db2 as well as open-source databases
- Long-maintained 2.x and 3.x lines for gradual upgrades
Limitations
- More concepts to learn (EntityManager, unit of work, proxies, flush) than Active Record ORMs like Eloquent
- Migrations require the separate Doctrine Migrations package
- More than 1,300 open GitHub issues
- Upgrading from 2.x to 3.x involves breaking changes
Who it suits
Good for
- Symfony applications
- PHP projects with complex domain models
- Enterprise PHP on Oracle or SQL Server
Look elsewhere if
- Laravel apps, which use Eloquent
- Small scripts where a query builder is enough
Alternatives to Doctrine ORM
| Tool | From | Free option | Stages |
|---|---|---|---|
| Entity Framework Core .NET ORM | — | ||
| jOOQ Type-safe SQL builder for the JVM | €12/mo | Free tier | |
| SQLAlchemy Python SQL toolkit and ORM | — | ||
| Active Record (Rails) Active Record ORM for Ruby on Rails | — | ||
| Eloquent ORM Active Record ORM for Laravel | — | ||
| Ent Code-generated Go entity framework | — |
Questions
Doctrine vs Eloquent: what is the difference?
Doctrine is a data mapper with a unit of work and plain PHP entities; Eloquent uses the Active Record pattern where models save themselves.
How do migrations work with Doctrine?
Install the separate doctrine/migrations package, which versions your schema and applies changes; Symfony integrates it through a bundle.
Which Doctrine ORM version is stable?
The project page lists 3.7 and 2.20 as stable, with 3.8, 2.21 and 4.0 upcoming.
Sources read for this page