PHP Doctrine 2 ORM

This course is currently not scheduled on the open calendar, but can be organized on request.

Number of days

3 day(s)


PHP developers and architects who want to ease their database development and increase their skills in persistence and ORM using frameworks such as Doctrine 2.


Having knowledge of PHP development. Basic knowledge of databases is required.


Learn how to use Doctrine 2 as the persistence layer for PHP applications.


Classroom training with hands-on exercises.


One of the most important concerns in any applications, is persistence. While the domain may be different, each application has data to be created, updated, retrieved and deleted. The importance of persistence is even more pronounced the more time is spent in developing the data layer of the application.

Doctrine 2 has become the most popular persistence system for PHP. It can be used as standalone, but also integrates well with Symfony2 and other popular frameworks. Doctrine is an ORM, an Object Relational Mapper. This technique enables developers to map their domain objects and attributes to relational database tables and columns. This lets them focus on the essentials, the domain, instead of queries and databases. During this course, you will learn how to use Doctrine to power your web applications and ease your database development.

This course will help you started with Doctrine and will give you an overview of its many features. The course begins by delving into the Doctrine mechanisms of entities and mapping, associations and inheritance. You will see how to create queries with DQL and the Query Builder. You will also learn how use the Doctrine DBAL to increase performance and execute native queries. The course follows up with a short introduction to PDO. To make it easier to deploy changes to your database schema, the course will conclude with an introduction to Doctrine Migrations. At the end of the course, you will be fluent in the use of Doctrine.

Instructor-led exercises will give you hands-on experience in using the Doctrine principles, so you can apply what you learned in your daily development.


  • Introduction

    • Agenda
    • What is Doctrine?
    • What is an ORM?
    • Doctrine packages
    • What are entities?
    • Entity states
    • Persistent fields
    • Mapping entities
    • The EntityManager
    • Transactional write-behind
    • EntityManager methods
    • Doctrine requirements
    • Integrating with Symfony2
    • Getting started with Doctrine
    • Installing the PHP runtime
    • Configuring PHP
    • Installing Composer
    • Installing Doctrine
    • Bootstrapping the application
    • Retrieving the EntityManager
    • Configuring Doctrine command-line tools
    • Generating the database schema
    • Exercise #01
  • Using Doctrine 2 ORM

    • Creating entities
    • Adding mapping information
    • Mapping with annotations
    • Generating getters and setters
    • Class-level annotations
    • Property-level annotations
    • More mapping annotations
    • Doctrine Mapping Types
    • Creating the database schema
    • Exercise #02
    • Persisting entities
    • Retrieving entities
    • Using repositories
    • Retrieving entities by id
    • Retrieving entities by simple conditions
    • Exercise #03
    • Updating entities
    • The Identity Map pattern
    • Removing entities
    • Detaching entities
    • Merging detached entities
    • Installing data fixtures
    • Creating a data fixture class
    • Implementing the load function
    • Creating a loader for the fixture class
    • Exercise #04
    • Optional Exercise #04b
    • Mapping associations
    • @ManyToOne and @OneToMany
    • Adding a fixture class for the association
    • Exercise #05
    • Optional Exercise #05b
    • Entity object graph traversal
    • Eager and lazy loading
    • @ManyToMany
    • Cascading or transitive persistence
    • Orphan removal
    • @JoinTable and @JoinColumn
    • Exercise #06
    • Optional Exercise #06b
    • Building queries with DQL
    • Creating custom repositories
    • Querying with Doctrine's Query Builder
    • Calling the custom repository method
    • Exercise #07
    • Inheritance mapping
    • Using mapped superclasses
    • Using single table inheritance
    • Using class table inheritance
    • Exercise #08
    • Managing events
    • Lifecycle callbacks
    • Using event listeners and event subscribers
    • Registering event listeners and subscribers
    • Exercise #09
    • Generating entities from an existing schema
    • Exercise #10
  • Using Doctrine DBAL

    • Using native queries
    • Exercise #11
    • Doctrine DBAL
    • Configuring DBAL
    • DBAL data retrieval
    • Using prepared statements
    • Using the QueryBuilder
    • Exercise #12
    • Transactions and concurrency
    • Implicit transaction demarcation
    • Explicit transaction demarcation
    • Exception handling and transactions
    • Versioning and optimistic locking
    • Important implementation notes
    • Exercise #13
    • Registering custom mapping types
    • Exercise #14
  • Using PDO

    • What is PDO?
    • Drivers
    • Constants
    • Connections
    • The PDO class
    • Transactions and autocommit
    • Transaction properties
    • PDOStatement
    • Prepared statements and stored procedures
    • Named placeholders
    • Nominal placeholders
    • Procedures with output parameters
    • Query
    • Scrollable cursor
    • Adhoc query
    • Adhoc SQL
    • Error handling
    • Creating DAOs
    • Exercise #15
  • Doctrine Migrations

    • Introduction to migrations
    • Setup
    • Configuration
    • Generating a migration
    • Migration class
    • Running migrations
    • Reverting migrations
    • Managing migrations
    • Writing migration SQL files
    • Managing the version table
    • Exercise #16
  • Best Practices

    • Constrain relationships as much as possible
    • Avoid composite keys
    • Use events judiciously
    • Use cascades judiciously
    • Don't use special characters
    • Don't use identifier quoting
    • Initialize collections in the constructor
    • Don¬ít map foreign keys to fields in an entity
    • Use explicit transaction demarcation
  • Summary and References

    • Summary
    • References