Doctrine (PHP)

Print Print
Reading time 4:51

Doctrine logo white.png
Stable release
2.8.2 / February 17, 2021; 3 months ago (2021-02-17)
Repository Edit this at Wikidata
Written inPHP
Operating systemCross-platform
TypeObject–relational mapping framework
LicenseMIT Edit this at Wikidata

The Doctrine Project (or Doctrine) is a set of PHP libraries primarily focused on providing persistence services and related functionality. Its prize projects are an object–relational mapper (ORM) and the database abstraction layer it is built on top of.

One of Doctrine's key features is the option to write database queries in Doctrine Query Language (DQL), an object-oriented dialect of SQL.

Developers of two major PHP frameworks, Symfony and Laminas have official out-of-the-box support for Doctrine, while 3rd party Doctrine packages are available for Laravel, CodeIgniter and others.

Usage demonstration

Entities in Doctrine 2 are lightweight PHP Objects that contain persistable properties. A persistable property is an instance variable of the entity that is saved into and retrieved from the database by Doctrine's data mapping capabilities via the Entity Manager - an implementation of the data mapper pattern:

 $user = new User();
 $user->name = "john2";
 $user->password = "doe";

 //$entityManager is an instance of Doctrine\ORM\EntityManagerInterface, usually obtained through dependency injection

 echo "The user with id $user->id has been saved.";

Doctrine 1.x follows the active record pattern for working with data, where a class corresponds with a database table. For instance, if a programmer wanted to create a new "User" object in a database, they would no longer need to write SQL queries, but instead could use the following PHP code:

 $user = new User();
 $user->name = "john";
 $user->password = "doe";
 echo "The user with id $user->id has been saved.";


Overview of Doctrine

One feature of Doctrine is the low level of configuration that is needed to start a project. Doctrine can generate object classes from an existing database, and the programmer can then specify relations and add custom functionality to the generated classes. There is no need to generate or maintain complex XML database schemas, as seen in many other frameworks.

Another key feature of Doctrine is the ability to optionally write database queries in an OO (object oriented) SQL dialect called DQL (Doctrine Query Language) inspired by Hibernate's HQL. Alternately, the QueryBuilder class (Doctrine_Query in Doctrine 1.x) allows one to construct queries through a fluent interface. These interfaces provide developers with powerful alternatives to SQL which maintain flexibility and still allow for switching of database back-ends, without requiring any code duplication.

Writing queries explicitly however is not always necessary, as Doctrine performs joins and fetches related objects automatically. Small projects can be easily constructed without writing queries.

Other notable features of Doctrine are:

  • support for hooks (methods which can validate or modify database input and output) and event listeners to structure business-related logic;
  • column aggregation inheritance (similar objects can be stored in one database table, with one type-column specifying the subtype of the particular object - the correct subclass is always returned when a query is done);
  • a caching framework, making use of several backends such as memcached, SQLite or APC;
  • ACID transactions;
  • database migrations;
  • a "compile" function to combine many PHP files of the framework into one, to avoid the performance hit usually incurred by including the many PHP files of a framework.


Doctrine was started by Konsta Vesterinen, also known as zYne-. The project's initial commit was made on April 13, 2006. As the project became more mature, the adoption began to pick up. Before long, the community was active and development was receiving regular contributions, among others from the Google Summer of Code project.

Doctrine 1.0.0 was released on September 1, 2008.[1]

The first stable version of Doctrine 2.0 was released on December 22, 2010, after 2.5 years of dedicated development starting in early 2008.[2]


Doctrine has been influenced by dozens of projects and many different people. The largest influences have been the Java ORM Hibernate and ActiveRecord from Ruby on Rails. Both of these ORM solutions have implemented a fully featured solution in the Java and Ruby languages. The purpose of the Doctrine project is to build an equally powerful solution for the PHP language for high-load websites that have to maintain a constant flow of visitors. Doctrine ORM can be used to improve the performance of such websites.

See also

  • Propel (PHP)
  • List of object–relational mapping software
  • Symfony, a web application framework which uses Doctrine by default
  • CodeIgniter, framework with integration officially supported by Doctrine team
  • DataEase, whose query language is also called DQL
  • Skipper, visualization tool for Doctrine
  • Method chaining


External links

Edited: 2021-06-18 14:11:55