The `symfony/orm-pack` is a Composer package designed to streamline the integration of Doctrine ORM (Object-Relational Mapping) into Symfony applications. It's essentially a 'meta-package' or 'pack' that pulls in several essential components and libraries required for robust database interaction using Doctrine.
Key components included in `symfony/orm-pack` are:
* `doctrine/doctrine-bundle`: Provides the Symfony integration layer for Doctrine ORM, allowing services like the `EntityManager` to be easily injected and configured.
* `doctrine/orm`: The core Doctrine ORM library itself, responsible for mapping PHP objects to database tables and vice-versa.
* `doctrine/dbal`: Doctrine's Database Abstraction Layer, which handles connections, queries, and schema management across different database vendors.
* `symfony/maker-bundle` (as a `dev` dependency): A powerful tool for rapidly generating common code like entities, repositories, controllers, and migrations, significantly speeding up development when working with Doctrine.
By requiring `symfony/orm-pack` (via `composer require symfony/orm-pack`), developers get a fully configured ORM solution out-of-the-box, saving time on initial setup and ensuring compatibility between different Doctrine and Symfony components. It enables features like:
* Defining database entities as plain PHP objects.
* Persisting, updating, and querying data using the `EntityManager`.
* Automatic database schema generation and migration management (via `make:migration` and `doctrine:migrations:migrate`).
* Using repositories for custom query logic.
This pack is the recommended starting point for any Symfony project that needs to interact with a relational database using an ORM.
Example Code
```php
<?php
// File: src/Entity/Product.php (after running 'php bin/console make:entity Product')
// This entity represents a 'products' table in your database.
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\ProductRepository;
#[ORM\Entity(repositoryClass: ProductRepository::class)]
class Product
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\Column(type: 'decimal', precision: 10, scale: 2)]
private ?string $price = null;
// -- Getters and Setters (typically generated by make:entity) --
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function getPrice(): ?string
{
return $this->price;
}
public function setPrice(string $price): static
{
$this->price = $price;
return $this;
}
}
// File: src/Controller/ProductController.php (example of using the Product entity)
// This controller demonstrates how to persist and retrieve Product objects.
namespace App\Controller;
use App\Entity\Product;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class ProductController extends AbstractController
{
#[Route('/product/new', name: 'app_product_new')]
public function new(EntityManagerInterface $entityManager): Response
{
$product = new Product();
$product->setName('Ultra HD Monitor');
$product->setPrice('399.99');
// Tells Doctrine to store the product in the database (it's not saved yet)
$entityManager->persist($product);
// Executes all pending operations (like INSERT statements) to the database
$entityManager->flush();
return new Response('<html><body>Product created with id: ' . $product->getId() . '</body></html>');
}
#[Route('/product/{id}', name: 'app_product_show')]
public function show(int $id, EntityManagerInterface $entityManager): Response
{
// Find a single product by its ID using the repository
$product = $entityManager->getRepository(Product::class)->find($id);
if (!$product) {
throw $this->createNotFoundException(
'No product found for id ' . $id
);
}
return new Response(
'<html><body>Product details: <br>'
. 'ID: ' . $product->getId() . '<br>'
. 'Name: ' . $product->getName() . '<br>'
. 'Price: $' . $product->getPrice() . '</body></html>'
);
}
}
/*
To make this code runnable in a Symfony project:
1. Ensure a Symfony project is set up.
2. Install the ORM pack: `composer require symfony/orm-pack`
3. Configure your database connection in `.env` (e.g., `DATABASE_URL="mysql://user:pass@127.0.0.1:3306/db_name?serverVersion=5.7"`)
4. Create the `Product` entity: `php bin/console make:entity Product`
(Follow the prompts to add `name` as `string` and `price` as `decimal` properties)
5. Generate a database migration file: `php bin/console make:migration`
6. Run the migration to create the `product` table in your database: `php bin/console doctrine:migrations:migrate`
7. Create the `ProductController.php` file with the content above.
8. Start your Symfony server (e.g., `symfony server:start`).
9. Access `/product/new` in your browser to create a new product entry.
10. Access `/product/{id}` (e.g., `/product/1`) to view the details of a specific product.
*/
```








symfony/orm-pack