You are browsing a version that has not yet been released. |
Getting Started with Doctrine
This guide covers getting started with the Doctrine ORM. After working through the guide you should know:
- How to install and configure Doctrine by connecting it to a database
- Mapping PHP objects to database tables
- Generating a database schema from PHP objects
- Using the
EntityManager
to insert, update, delete and find objects in the database.
Guide Assumptions
This guide is designed for beginners that haven't worked with Doctrine ORM before. There are some prerequisites for the tutorial that have to be installed:
- PHP (latest stable version)
- Composer Package Manager (Install Composer)
The code of this tutorial is available on Github.
What is Doctrine?
Doctrine ORM is an object-relational mapper (ORM) for PHP 7.1+ that provides transparent persistence for PHP objects. It uses the Data Mapper pattern at the heart, aiming for a complete separation of your domain/business logic from the persistence in a relational database management system.
The benefit of Doctrine for the programmer is the ability to focus on the object-oriented business logic and worry about persistence only as a secondary problem. This doesn't mean persistence is downplayed by Doctrine 2, however it is our belief that there are considerable benefits for object-oriented programming if persistence and entities are kept separated.
What are Entities?
Entities are PHP Objects that can be identified over many requests by a unique identifier or primary key. These classes don't need to extend any abstract base class or interface.
An entity contains 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.
An entity class must not be final nor read-only, although it can contain final methods or read-only properties.
An Example Model: Bug Tracker
For this Getting Started Guide for Doctrine we will implement the Bug Tracker domain model from the Zend_Db_Table documentation. Reading their documentation we can extract the requirements:
- A Bug has a description, creation date, status, reporter and engineer
- A Bug can occur on different Products (platforms)
- A Product has a name.
- Bug reporters and engineers are both Users of the system.
- A User can create new Bugs.
- The assigned engineer can close a Bug.
- A User can see all their reported or assigned Bugs.
- Bugs can be paginated through a list-view.
Project Setup
Create a new empty folder for this tutorial project, for example
doctrine2-tutorial
and create a new file composer.json
inside
that directory with the following contents:
{
"require": {
"doctrine/orm": "^2.11.0",
"doctrine/dbal": "^3.2",
"symfony/yaml": "^5.4",
"symfony/cache": "^5.4"
},
"autoload": {
"psr-0": {"": "src/"}
}
}
Install Doctrine using the Composer Dependency Management tool, by calling:
$ composer install
This will install the packages Doctrine Common, Doctrine DBAL, Doctrine ORM,
into the vendor
directory.
Add the following directories:
doctrine2-tutorial
|-- config
| `-- xml
| `-- yaml
`-- src
The YAML driver is deprecated and will be removed in version 3.0. It is strongly recommended to switch to one of the other mappings. |
It is strongly recommended that you require |
Obtaining the EntityManager
Doctrine's public interface is through the EntityManager
. This class
provides access points to the complete lifecycle management for your entities,
and transforms entities from and back to persistence. You have to
configure and create it to use your entities with Doctrine ORM. I
will show the configuration steps and then discuss them step by
step:
1 <?php
// bootstrap.php
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMSetup;
require_once "vendor/autoload.php";
// Create a simple "default" Doctrine ORM configuration for Attributes
$config = ORMSetup::createAttributeMetadataConfiguration(
paths: [__DIR__ . '/src'],
isDevMode: true,
);
// or if you prefer annotation, YAML or XML
// $config = ORMSetup::createAnnotationMetadataConfiguration(
// paths: array(__DIR__."/src"),
// isDevMode: true,
// );
// $config = ORMSetup::createXMLMetadataConfiguration(
// paths: [__DIR__ . '/config/xml'],
// isDevMode: true,
//);
// $config = ORMSetup::createYAMLMetadataConfiguration(
// paths: array(__DIR__."/config/yaml"),
// isDevMode: true,
// );
// configuring the database connection
$connection = DriverManager::getConnection([
'driver' => 'pdo_sqlite',
'path' => __DIR__ . '/db.sqlite',
], $config);
// obtaining the entity manager
$entityManager = new EntityManager($connection, $config);
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
The YAML driver is deprecated and will be removed in version 3.0. It is strongly recommended to switch to one of the other mappings. |
The require_once
statement sets up the class autoloading for Doctrine and
its dependencies using Composer's autoloader.
The second block consists of the instantiation of the ORM
Configuration
object using the ORMSetup
helper. It assumes a bunch
of defaults that you don't have to bother about for now. You can
read up on the configuration details in the
reference chapter on configuration.
The third block shows the configuration options required to connect to a database. In this case, we'll use a file-based SQLite database. All the configuration options for all the shipped drivers are given in the DBAL Configuration section of the manual.
The last block shows how the EntityManager
is obtained from a
factory method.
Generating the Database Schema
Doctrine has a command-line interface that allows you to access the SchemaTool, a component that can generate a relational database schema based entirely on the defined entity classes and their metadata. For this tool to work, you need to create an executable console script as described in the tools chapter.
If you created the bootstrap.php
file as described in the previous section,
that script could look like this:
1 #!/usr/bin/env php
<?php
// bin/doctrine
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
// Adjust this path to your actual bootstrap.php
require __DIR__ . 'path/to/your/bootstrap.php';
ConsoleRunner::run(
new SingleManagerProvider($entityManager)
);
2
3
4
5
6
7
8
9
10
11
12
13
In the following examples, we will assume that this script has been created as
bin/doctrine
.
$ php bin/doctrine orm:schema-tool:create
Since we haven't added any entity metadata in src
yet, you'll see a message
stating "No Metadata Classes to process." In the next section, we'll create a
Product entity along with the corresponding metadata, and run this command again.
Note that as you modify your entities' metadata during the development process, you'll need to update your database schema to stay in sync with the metadata. You can easily recreate the database using the following commands:
$ php bin/doctrine orm:schema-tool:drop --force
$ php bin/doctrine orm:schema-tool:create
Or you can use the update functionality:
$ php bin/doctrine orm:schema-tool:update --force
The updating of databases uses a diff algorithm for a given
database schema. This is a cornerstone of the Doctrine\DBAL
package,
which can even be used without the Doctrine ORM package.
Starting with the Product Entity
We start with the simplest entity, the Product. Create a src/Product.php
file to contain the Product
entity definition:
When creating entity classes, all of the fields should be private
.
Use protected
when strictly needed and very rarely if not ever public
.
Adding behavior to Entities
There are two options to define methods in entities: getters/setters, or mutators and DTOs, respectively for anemic entities or rich entities.
Anemic entities: Getters and setters
The most popular method is to create two kinds of methods to read (getter) and update (setter) the object's properties.
The id field has no setter since, generally speaking, your code should not set this value since it represents a database id value. (Note that Doctrine itself can still set the value using the Reflection API instead of a defined setter function.)
Doctrine ORM does not use any of the methods you defined: it uses
reflection to read and write values to your objects, and will never
call methods, not even |
This approach is mostly used when you want to focus on behavior-less entities, and when you want to have all your business logic in your services rather than in the objects themselves.
Getters and setters are a common convention which makes it possible to expose each field of your entity to the external world, while allowing you to keep some type safety in place.
Such an approach is a good choice for RAD (rapid application development), but may lead to problems later down the road, because providing such an easy way to modify any field in your entity means that the entity itself cannot guarantee validity of its internal state. Having any object in invalid state is dangerous:
- An invalid state can bring bugs in your business logic.
- The state can be implicitly saved in the database: any forgotten
flush
can persist the broken state. - If persisted, the corrupted data will be retrieved later in your application when the data is loaded again, thereby leading to bugs in your business logic.
- When bugs occur after corrupted data is persisted, troubleshooting will become much harder, and you might be aware of the bug too late to fix it in a proper manner.
implicitly saved in database, thereby leading to corrupted or inconsistent data in your storage, and later in your application when the data is loaded again.
This method, although very common, is inappropriate for Domain Driven Design (DDD) where methods should represent real business operations and not simple property change, And business invariants should be maintained both in the application state (entities in this case) and in the database, with no space for data corruption. |
Here is an example of a simple anemic entity:
1 <?php
class User
{
private $username;
private $passwordHash;
private $bans;
public function getUsername(): string
{
return $this->username;
}
public function setUsername(string $username): void
{
$this->username = $username;
}
public function getPasswordHash(): string
{
return $this->passwordHash;
}
public function setPasswordHash(string $passwordHash): void
{
$this->passwordHash = $passwordHash;
}
public function getBans(): array
{
return $this->bans;
}
public function addBan(Ban $ban): void
{
$this->bans[] = $ban;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
In the example above, we avoid all possible logic in the entity and only care about putting and retrieving data into it without validation (except the one provided by type-hints) nor consideration about the object's state.
As Doctrine ORM is a persistence tool for your domain, the state of an object is really important. This is why we strongly recommend using rich entities.
Rich entities: Mutators and DTOs
We recommend using a rich entity design and rely on more complex mutators, and if needed based on DTOs. In this design, you should not use getters nor setters, and instead, implement methods that represent the behavior of your domain.
For example, when having a User
entity, we could foresee
the following kind of optimization.
Example of a rich entity with proper accessors and mutators:
1 <?php
class User
{
private $banned;
private $username;
private $passwordHash;
private $bans;
public function toNickname(): string
{
return $this->username;
}
public function authenticate(string $password, callable $checkHash): bool
{
return $checkHash($password, $this->passwordHash) && ! $this->hasActiveBans();
}
public function changePassword(string $password, callable $hash): void
{
$this->passwordHash = $hash($password);
}
public function ban(\DateInterval $duration): void
{
assert($duration->invert !== 1);
$this->bans[] = new Ban($this);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Please note that this example is only a stub. When going further in the documentation, we will update this object with more behavior and maybe update some methods. |
The entities should only mutate state after checking that all business logic
invariants are being respected.
Additionally, our entities should never see their state change without
validation. For example, creating a new Product()
object without any data
makes it an invalid object.
Rich entities should represent behavior, not data, therefore
they should be valid even after a __construct()
call.
To help creating such objects, we can rely on DTOs
, and/or make
our entities always up-to-date. This can be performed with static constructors,
or rich mutators that accept DTOs
as parameters.
The role of the DTO
is to maintain the entity's state and to help us rely
upon objects that correctly represent the data that is used to mutate the
entity.
A DTO is an object
that only carries data without any logic. Its only goal is to be transferred
from one service to another.
A |
By using DTOs
, if we take our previous User
example, we could create
a ProfileEditingForm
DTO that will be a plain model, totally unrelated to
our database, that will be populated via a form and validated.
Then we can add a new mutator to our User
:
There are several advantages to using such a model:
- Entity state is always valid. Since no setters exist, this means that we
only update portions of the entity that should already be valid.
- Instead of having plain getters and setters, our entity now has
real behavior: it is much easier to determine the logic in the domain.
- DTOs can be reused in other components, for example deserializing mixed
content, using forms...
- Classic and static constructors can be used to manage different ways to
create our objects, and they can also use DTOs.
- Anemic entities tend to isolate the entity from logic, whereas rich
entities allow putting the logic in the object itself, including data validation.
The next step for persistence with Doctrine is to describe the structure of
the Product
entity to Doctrine using a metadata language. The metadata
language describes how entities, their properties and references should be
persisted and what constraints should be applied to them.
Metadata for an Entity can be configured using attributes directly in the Entity class itself, or in an external XML or YAML file. This Getting Started guide will demonstrate metadata mappings using all three methods, but you only need to choose one.
1 <?php
// src/Product.php
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\Table(name: 'products')]
class Product
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue]
private int|null $id = null;
#[ORM\Column(type: 'string')]
private string $name;
// .. (other code)
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
The YAML driver is deprecated and will be removed in version 3.0. It is strongly recommended to switch to one of the other mappings.
|
The top-level entity
definition specifies information about
the class and table name. The primitive type Product#name
is
defined as a field
attribute. The id
property is defined with
the id
tag. It has a generator
tag nested inside, which
specifies that the primary key generation mechanism should automatically
use the database platform's native id generation strategy (for
example, AUTO INCREMENT in the case of MySql, or Sequences in the
case of PostgreSql and Oracle).
Now that we have defined our first entity and its metadata, let's update the database schema:
$ php bin/doctrine orm:schema-tool:update --force --dump-sql
Specifying both flags --force
and --dump-sql
will cause the DDL
statements to be executed and then printed to the screen.
Now, we'll create a new script to insert products into the database:
1 <?php
// create_product.php <name>
require_once "bootstrap.php";
$newProductName = $argv[1];
$product = new Product();
$product->setName($newProductName);
$entityManager->persist($product);
$entityManager->flush();
echo "Created Product with ID " . $product->getId() . "\n";
2
3
4
5
6
7
8
9
10
11
12
13
Call this script from the command-line to see how new products are created:
$ php create_product.php ORM
$ php create_product.php DBAL
What is happening here? Using the Product
class is pretty standard OOP.
The interesting bits are the use of the EntityManager
service. To
notify the EntityManager that a new entity should be inserted into the database,
you have to call persist()
. To initiate a transaction to actually perform
the insertion, you have to explicitly call flush()
on the EntityManager
.
This distinction between persist and flush is what allows the aggregation of
all database writes (INSERT, UPDATE, DELETE) into one single transaction, which
is executed when flush()
is called. Using this approach, the write-performance
is significantly better than in a scenario in which writes are performed on
each entity in isolation.
Next, we'll fetch a list of all the Products in the database. Let's create a new script for this:
The EntityManager#getRepository()
method can create a finder object (called
a repository) for every type of entity. It is provided by Doctrine and contains
some finder methods like findAll()
.
Let's continue by creating a script to display the name of a product based on its ID:
Next we'll update a product's name, given its id. This simple example will
help demonstrate Doctrine's implementation of the UnitOfWork pattern. Doctrine
keeps track of all the entities that were retrieved from the Entity Manager,
and can detect when any of those entities' properties have been modified.
As a result, rather than needing to call persist($entity)
for each individual
entity whose properties were changed, a single call to flush()
at the end of a
request is sufficient to update the database for all of the modified entities.
1 <?php
// update_product.php <id> <new-name>
require_once "bootstrap.php";
$id = $argv[1];
$newName = $argv[2];
$product = $entityManager->find('Product', $id);
if ($product === null) {
echo "Product $id does not exist.\n";
exit(1);
}
$product->setName($newName);
$entityManager->flush();
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
After calling this script on one of the existing products, you can verify the
product name changed by calling the show_product.php
script.
Adding Bug and User Entities
We continue with the bug tracker example by creating the Bug
and User
classes. We'll store them in src/Bug.php
and src/User.php
, respectively.
1 <?php
// src/Bug.php
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\Table(name: 'bugs')]
class Bug
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue]
private int|null $id;
#[ORM\Column(type: 'string')]
private string $description;
#[ORM\Column(type: 'datetime')]
private DateTime $created;
#[ORM\Column(type: 'string')]
private string $status;
public function getId(): int|null
{
return $this->id;
}
public function getDescription(): string
{
return $this->description;
}
public function setDescription(string $description): void
{
$this->description = $description;
}
public function setCreated(DateTime $created)
{
$this->created = $created;
}
public function getCreated(): DateTime
{
return $this->created;
}
public function setStatus($status): void
{
$this->status = $status;
}
public function getStatus():string
{
return $this->status;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
1 <?php
// src/User.php
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\Table(name: 'users')]
class User
{
/** @var int */
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private int|null $id = null;
/** @var string */
#[ORM\Column(type: 'string')]
private string $name;
public function getId(): int|null
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
All of the properties we've seen so far are of simple types (integer, string, and datetime). But now, we'll add properties that will store objects of specific entity types in order to model the relationships between different entities.
At the database level, relationships between entities are represented by foreign keys. But with Doctrine, you'll never have to (and never should) work with the foreign keys directly. You should only work with objects that represent foreign keys through their own identities.
For every foreign key you either have a Doctrine ManyToOne or OneToOne association. On the inverse sides of these foreign keys you can have OneToMany associations. Obviously you can have ManyToMany associations that connect two tables with each other through a join table with two foreign keys.
Now that you know the basics about references in Doctrine, we can extend the domain model to match the requirements:
1 <?php
// src/Bug.php
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
class Bug
{
// ... (previous code)
/** @var Collection<int, Product> */
private Collection $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1 <?php
// src/User.php
use Doctrine\Common\Collections\ArrayCollection;
class User
{
// ... (previous code)
/** @var Collection<int, Bug> */
private Collection $reportedBugs;
/** @var Collection<int, Bug> */
private Collection $assignedBugs;
public function __construct()
{
$this->reportedBugs = new ArrayCollection();
$this->assignedBugs = new ArrayCollection();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Whenever an entity is created from the database, a |
Because we only work with collections for the references we must be careful to implement a bidirectional reference in the domain model. The concept of owning or inverse side of a relation is central to this notion and should always be kept in mind. The following assumptions are made about relations and have to be followed to be able to work with Doctrine ORM. These assumptions are not unique to Doctrine ORM but are best practices in handling database relations and Object-Relational Mapping.
- In a one-to-one relation, the entity holding the foreign key of the related entity on its own database table is always the owning side of the relation.
- In a many-to-one relation, the Many-side is the owning side by default because it holds the foreign key. Accordingly, the One-side is the inverse side by default.
- In a many-to-one relation, the One-side can only be the owning side if the relation is implemented as a ManyToMany with a join table, and the One-side is restricted to allow only UNIQUE values per database constraint.
- In a many-to-many relation, both sides can be the owning side of the relation. However, in a bi-directional many-to-many relation, only one side is allowed to be the owning side.
- Changes to Collections are saved or updated, when the entity on the owning side of the collection is saved or updated.
- Saving an Entity at the inverse side of a relation never triggers a persist operation to changes to the collection.
Consistency of bi-directional references on the inverse side of a relation have to be managed in userland application code. Doctrine cannot magically update your collections to be consistent. |
In the case of Users and Bugs we have references back and forth to the assigned and reported bugs from a user, making this relation bi-directional. We have to change the code to ensure consistency of the bi-directional reference:
1 <?php
// src/Bug.php
class Bug
{
// ... (previous code)
private User $engineer;
private User $reporter;
public function setEngineer(User $engineer): void
{
$engineer->assignedToBug($this);
$this->engineer = $engineer;
}
public function setReporter(User $reporter): void
{
$reporter->addReportedBug($this);
$this->reporter = $reporter;
}
public function getEngineer(): User
{
return $this->engineer;
}
public function getReporter(): User
{
return $this->reporter;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1 <?php
// src/User.php
class User
{
// ... (previous code)
/** @var Collection<int, Bug> */
private Collection $reportedBugs;
/** @var Collection<int, Bug> */
private Collection $assignedBugs;
public function addReportedBug(Bug $bug): void
{
$this->reportedBugs[] = $bug;
}
public function assignedToBug(Bug $bug): void
{
$this->assignedBugs[] = $bug;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
I chose to name the inverse methods in past-tense, which should indicate that the actual assigning has already taken place and the methods are only used for ensuring consistency of the references. This approach is my personal preference, you can choose whatever method to make this work.
You can see from User#addReportedBug()
and
User#assignedToBug()
that using this method in userland alone
would not add the Bug to the collection of the owning side in
Bug#reporter
or Bug#engineer
. Using these methods and
calling Doctrine for persistence would not update the Collections'
representation in the database.
Only using Bug#setEngineer()
or Bug#setReporter()
correctly saves the relation information.
The Bug#reporter
and Bug#engineer
properties are
Many-To-One relations, which point to a User. In a normalized
relational model, the foreign key is saved on the Bug's table, hence
in our object-relation model the Bug is at the owning side of the
relation. You should always make sure that the use-cases of your
domain model should drive which side is an inverse or owning one in
your Doctrine mapping. In our example, whenever a new bug is saved
or an engineer is assigned to the bug, we don't want to update the
User to persist the reference, but the Bug. This is the case with
the Bug being at the owning side of the relation.
Bugs reference Products by a uni-directional ManyToMany relation in the database that points from Bugs to Products.
1 <?php
// src/Bug.php
class Bug
{
// ... (previous code)
/** @var Collection<int, Product> */
private Collection $products;
public function assignToProduct(Product $product): void
{
$this->products[] = $product;
}
/** @return Collection<int, Product> */
public function getProducts(): Collection
{
return $this->products;
}
}
2