Doctrine ORM for PHP: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Doctrine ORM and why is it needed?

\n\n

Doctrine ORM (Object-Relational Mapping) is a powerful library for PHP that allows developers to work with databases using an object-oriented approach instead of writing raw SQL queries. Doctrine ORM is the de facto standard for Symfony, but can also be used in any other PHP project (Laravel, Yii, plain PHP).

\n\n

The main problem that Doctrine solves: the mismatch between the relational data model (tables, rows, columns) and the object model (classes, objects, properties). Doctrine automatically converts PHP objects into database records and vice versa.

\n\n

Key features:

\n
    \n
  • Automatic database schema creation — based on annotations/attributes in classes
  • \n
  • Repository Pattern — convenient methods for searching and filtering data
  • \n
  • DQL (Doctrine Query Language) — an object-oriented query language
  • \n
  • Lazy Loading — loading related data only when needed
  • \n
  • Migrations — managing database schema changes
  • \n
  • Transactions — full ACID support
  • \n
  • Caching — metadata, queries, results
  • \n
\n\n

Doctrine ORM is suitable for projects of any scale: from small websites to enterprise systems with hundreds of tables.

\n\n

Installing Doctrine ORM

\n\n

Installation is done via Composer (PHP package manager). Open a terminal in your project root and run:

\n\n
composer require doctrine/orm
\n\n

For working with Symfony, it is recommended to install the DoctrineBundle package:

\n\n
composer require symfony/orm-pack\ncomposer require --dev symfony/maker-bundle  # for code generation
\n\n

If you are using plain PHP without a framework, you will additionally need to install a driver for your database:

\n\n
composer require doctrine/dbal\ncomposer require doctrine/annotations  # for working with annotations (PHP 7.4 and below)
\n\n

For working with PHP 8+ attributes (recommended method): annotations are not needed, Doctrine supports native attributes.

\n\n

Quick Start: Code Example

\n\n

Let's look at a simple example — creating a "Product" entity and saving it to the database.

\n\n

1. Create the Entity class

\n\n
<?php\n// src/Entity/Product.php\nnamespace App\\Entity;\n\nuse Doctrine\\ORM\\Mapping as ORM;\n\n#[ORM\\Entity]\n#[ORM\\Table(name: 'products')]\nclass Product\n{\n    #[ORM\\Id]\n    #[ORM\\GeneratedValue]\n    #[ORM\\Column(type: 'integer')]\n    private int $id;\n\n    #[ORM\\Column(type: 'string', length: 255)]\n    private string $name;\n\n    #[ORM\\Column(type: 'float')]\n    private float $price;\n\n    // Getters and setters\n    public function getId(): int\n    {\n        return $this->id;\n    }\n\n    public function getName(): string\n    {\n        return $this->name;\n    }\n\n    public function setName(string $name): self\n    {\n        $this->name = $name;\n        return $this;\n    }\n\n    public function getPrice(): float\n    {\n        return $this->price;\n    }\n\n    public function setPrice(float $price): self\n    {\n        $this->price = $price;\n        return $this;\n    }\n}
\n\n

2. Create and save the object

\n\n
<?php\n// In a Symfony controller or anywhere else\nuse App\\Entity\\Product;\nuse Doctrine\\ORM\\EntityManagerInterface;\n\nclass ProductController\n{\n    public function createProduct(EntityManagerInterface $entityManager)\n    {\n        $product = new Product();\n        $product->setName('Laptop');\n        $product->setPrice(49999.99);\n\n        // Tell Doctrine that the object needs to be saved\n        $entityManager->persist($product);\n        \n        // Execute the INSERT                    

Recommendations