The Doctrine Project (or Doctrine) is a set of PHP libraries primarily focused on providing
persistence
Persistence or Persist may refer to:
Math and computers
* Image persistence, in LCD monitors
* Persistence (computer science), the characteristic of data that outlives the execution of the program that created it
* Persistence of a number, a ma ...
services and related functionality. Its most commonly known projects are the
object–relational mapper (ORM) and the
database abstraction layer
A database abstraction layer (DBAL or DAL) is an application programming interface which unifies the communication between a computer application and databases such as SQL Server, IBM Db2, MySQL, PostgreSQL, Oracle or SQLite. Traditionally, all ...
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
Symfony is a free and open-source PHP web application framework and a set of reusable PHP component libraries. It was published as free software on October 18, 2005, and released under the MIT License.
Goal
Symfony aims to speed up the crea ...
and
Laminas have official out-of-the-box support for Doctrine, while 3rd party Doctrine packages are available for
Laravel
Laravel is a open-source software, free and open-source PHP-based web framework for building web applications. It was created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) ar ...
,
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
In software engineering, the data mapper pattern is an architectural pattern. It was named by Martin Fowler in his 2003 book ''Patterns of Enterprise Application Architecture''. The interface of an object conforming to this pattern would include f ...
:
$user = new User();
$user->name = "john2";
$user->password = "doe";
//$entityManager is an instance of Doctrine\ORM\EntityManagerInterface, usually obtained through dependency injection
$entityManager->persist($user);
$entityManager->flush();
echo "The user with id $user->id has been saved.";
Doctrine 1.x follows the
active record pattern
In software engineering, the active record pattern is an architectural pattern. It is found in software that stores in-memory object data in relational databases. It was named by Martin Fowler in his 2003 book ''Patterns of Enterprise Application ...
for working with data, where a
class
Class, Classes, or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used d ...
corresponds with a
database table
In a database, a table is a collection of related data organized in table format; consisting of columns and rows.
In relational databases, and flat file databases, a ''table'' is a set of data elements (values) using a model of vertical colum ...
. For instance, if a programmer wanted to create a new "User" object in a database, they would no longer need to write
SQL
Structured Query Language (SQL) (pronounced ''S-Q-L''; or alternatively as "sequel")
is a domain-specific language used to manage data, especially in a relational database management system (RDBMS). It is particularly useful in handling s ...
queries, but instead could use the following PHP code:
$user = new User();
$user->name = "john";
$user->password = "doe";
$user->save();
echo "The user with id $user->id has been saved.";
Features
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
Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing data. It defines a set of rules for encoding electronic document, documents in a format that is both human-readable and Machine-r ...
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
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impleme ...
) SQL dialect called DQL (Doctrine Query Language) inspired by
Hibernate's HQL. Alternately, the class ( in Doctrine 1.x) allows one to construct queries through a
fluent interface
In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). The term was coined in 2005 by Eric ...
. 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 Join may refer to:
* Join (law), to include additional counts or additional defendants on an indictment
*In mathematics:
** Join (mathematics), a least upper bound of sets orders in lattice theory
** Join (topology), an operation combining two top ...
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
Memcached (pronounced variously /mɛmkæʃˈdiː/ ''mem-cash-dee'' or /ˈmɛmkæʃt/ ''mem-cashed'') is a general-purpose distributed memory-caching system. It is often used to speed up dynamic database-driven websites by caching data and object ...
,
SQLite
SQLite ( "S-Q-L-ite", "sequel-ite") is a free and open-source relational database engine written in the C programming language. It is not a standalone app; rather, it is a library that software developers embed in their apps. As such, it ...
or
APC;
*
ACID
An acid is a molecule or ion capable of either donating a proton (i.e. Hydron, hydrogen cation, H+), known as a Brønsted–Lowry acid–base theory, Brønsted–Lowry acid, or forming a covalent bond with an electron pair, known as a Lewis ...
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.
History
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
The Google Summer of Code, often abbreviated to GSoC, is an international annual program in which Google awards stipends to contributors who successfully complete a free and open-source software coding project during the summer. , the program is ...
project.
Doctrine 1.0.0 was released on September 1, 2008.
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.
Doctrine 2 First Stable Release
/ref>
Influences
Doctrine has been influenced by dozens of projects and many different people. The largest influences have been the Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
ORM solution Hibernate
Hibernation is a state of minimal activity and metabolic reduction entered by some animal species. Hibernation is a seasonal heterothermy characterized by low body-temperature, slow breathing and heart-rate, and low metabolic rate. It is most ...
and the ActiveRecord architecture used in Ruby on Rails
Ruby on Rails (simplified as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web pa ...
. The purpose of the Doctrine project is to build an equally powerful solution for the PHP
PHP is a general-purpose scripting language geared towards web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by the PHP Group. ...
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
*List of object–relational mapping software
This is a list of well-known object–relational mapping software.
Java
* Apache Cayenne, open-source for Java
*Apache OpenJPA, open-source for Java
* DataNucleus, open-source JDO and JPA implementation (formerly known as JPOX)
* Ebean, open-sourc ...
*Symfony
Symfony is a free and open-source PHP web application framework and a set of reusable PHP component libraries. It was published as free software on October 18, 2005, and released under the MIT License.
Goal
Symfony aims to speed up the crea ...
, a web application framework
A web framework (WF) or web application framework (WAF) is a software framework that is designed to support the development of web applications including web services, web resources, and web APIs. Web frameworks provide a standard way to build and ...
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
References
External links
*{{Official website
PHP software
PHP libraries
Object–relational mapping