The Doctrine Project (or Doctrine) is a set of PHP libraries primarily focused on providing
persistence 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 creat ...
and
Laminas
Laminas Project (formerly Zend Framework or ZF) is an open source, object-oriented web application framework implemented in PHP 7 and licensed under the New BSD License. The framework is basically a collection of professional PHP-based packag ...
have official out-of-the-box support for Doctrine, while 3rd party Doctrine packages are available for
Laravel
Laravel is a free and open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern and based on Symfony. Some of the feature ...
,
CodeIgniter
CodeIgniter is an open-source software rapid development web framework, for use in building dynamic web sites with PHP.
Popularity
CodeIgniter is loosely based on the popular model–view–controller (MVC) development pattern. While controller c ...
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 fu ...
:
$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 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 differently ...
corresponds with a
database table
A table is a collection of related data held in a table format within a database. It consists 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 col ...
. 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";
$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 arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. ...
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", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
) 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 topo ...
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 ''mem-cash-dee'' or ''mem-cashed'') is a general-purpose distributed memory caching, memory-caching system. It is often used to speed up dynamic database-driven websites by caching data and Object (computer science) ...
,
SQLite
SQLite (, ) is a 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 belongs to the family of embedded databases. It is the mo ...
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.
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 (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
ORM solution Hibernate
Hibernation is a state of minimal activity and metabolic depression undergone by some animal species. Hibernation is a seasonal heterothermy characterized by low body-temperature, slow breathing and heart-rate, and low metabolic rate. It 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 ...
. The purpose of the Doctrine project is to build an equally powerful solution for the PHP
PHP is a General-purpose programming language, general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementati ...
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)
Propel is a free, open-source ( MIT) object–relational mapping toolkit written in PHP. It is also an integral part of the PHP framework Symfony and was the default ORM up to, and including version 1.2.
History
The Propel project was starte ...
*List of object–relational mapping software
This is a list of well-known object–relational mapping software. It is not up-to-date or all-inclusive.
Java
*Apache Cayenne, open-source for Java
*Apache OpenJPA, open-source for Java
* DataNucleus, open-source JDO and JPA implementation (forme ...
*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 creat ...
, 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
CodeIgniter is an open-source software rapid development web framework, for use in building dynamic web sites with PHP.
Popularity
CodeIgniter is loosely based on the popular model–view–controller (MVC) development pattern. While controller c ...
, framework with integration officially supported by Doctrine team
* DataEase, whose query language is also called DQL
* Skipper, visualization tool for Doctrine
*Method chaining
Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement ...
References
External links
*{{Official website
PHP software
PHP libraries
Object-relational mapping