What is Doctrine ORM and why is it needed?
\n\nDoctrine 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\nThe 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\nKey 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
Doctrine ORM is suitable for projects of any scale: from small websites to enterprise systems with hundreds of tables.
\n\nInstalling Doctrine ORM
\n\nInstallation is done via Composer (PHP package manager). Open a terminal in your project root and run:
\n\ncomposer require doctrine/orm\n\nFor working with Symfony, it is recommended to install the DoctrineBundle package:
\n\ncomposer require symfony/orm-pack\ncomposer require --dev symfony/maker-bundle # for code generation\n\nIf you are using plain PHP without a framework, you will additionally need to install a driver for your database:
\n\ncomposer require doctrine/dbal\ncomposer require doctrine/annotations # for working with annotations (PHP 7.4 and below)\n\nFor working with PHP 8+ attributes (recommended method): annotations are not needed, Doctrine supports native attributes.
\n\nQuick Start: Code Example
\n\nLet's look at a simple example — creating a "Product" entity and saving it to the database.
\n\n1. 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\n2. 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 Also in library
Working with CSV in Rust: csv, serde libraries and efficient data processing
fx for Go: Complete Guide with Examples
Sending email in Go (Golang): libraries and code examples
Redis client for C# (.NET): StackExchange.Redis — connection and examples