Doctrine Bundle Documentation
repository·3.3.x·Indexed 26 days ago
https://github.com/doctrine/doctrinebundleOfficial integration of Doctrine DBAL and ORM for the Symfony Framework. Provides configuration for database persistence, including DBAL connections, ORM entity managers, custom ID generators (UUID/ULID), mapping options, and RDBMS-specific settings for PostgreSQL, MySQL, Oracle, SQLite, IBM DB2, and SQL Anywhere.
What's inside DoctrineBundle
- Doctrine Bundle provides the integration for Doctrine DBAL (Database Abstraction Layer) and Doctrine ORM (Object Relational Mapper) within the Symfony Framework. It allows Symfony applications to leverage Doctrine's persistence services, including the ability to use Doctrine Query Language (DQL) for object-oriented database queries.
Integrate Doctrine ORM and DBAL into Symfony
3.3.xDoctrineBundle provides the integration layer for using Doctrine's Object-Relational Mapper (ORM) and Database Abstraction Layer (DBAL) within a Symfony application. It enables configuration of database connections, provides CLI commands for database management, and integrates with the Symfony web debug toolbar.Autowire specific Doctrine DBAL connections
3.3.xWhen using multiple connections, you can autowire a specific connection into your services by using a specific type-hinting pattern:
Doctrine\DBAL\Connection $<connection_name>Connection.For example, if you have a connection named
purchase_logs, you should type-hint it as$purchaseLogsConnection(using camelCase for the variable name) to ensure the correct connection is injected.- public function __construct(Connection $connection) + public function __construct(Connection $purchaseLogsConnection) { $this->connection = $purchaseLogsConnection; }Enable DoctrineBundle manually in config/bundles.php
3.3.xIf you are not using Symfony Flex, you must manually register the bundle by adding
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::classto yourconfig/bundles.phpfile with the'all' => trueconfiguration.<?php // config/bundles.php return [ // ... Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true], // ... ];Remove deprecated enable_native_lazy_objects configuration
3.3.xThe configuration optiondoctrine.orm.entity_managers.{em_name}.enable_native_lazy_objectsis deprecated in DoctrineBundle 3.1 and will be removed in version 4.0. Native lazy objects are now always enabled by default. You should remove this key from your configuration to prepare for the 4.0 upgrade.Enable the new `report_fields_where_declared` mapping driver mode
3.3.xDoctrine ORM 2.16+ changed how annotation and attribute mapping drivers report inherited fields. To avoid deprecation notices and prepare for ORM 3.0 (where this mode becomes mandatory), you can opt-in to the new mode in DoctrineBundle 2.10+ by setting
report_fields_where_declaredtotruein your entity manager configuration. This setting only affects mapping configurations using attributes or annotations.Note: While this should not affect valid use cases, it may trigger
MappingExceptions if your current configuration relies on unsupported inheritance patterns.Upgrade to DoctrineBundle 3.0
3.3.xWhen upgrading from version 2.x to 3.0, note the following breaking changes:
- PHP Version: The minimum required PHP version is now 8.4.
- Caching: Configuring caching options to use services backed by
doctrine/cacheis no longer supported. Migrate to PSR-6 services instead. - Dropped Package Support:
doctrine/dbal3doctrine/persistence3doctrine/orm2psr/log1 and 2twig/twig2
- ORM Changes: Support for YML and annotation metadata drivers is dropped.
LazyServiceEntityRepositoryhas been removed.
Register custom DBAL types using the AsDbalType attribute
3.3.xYou can automatically register custom DBAL types by adding the
#[AsDbalType]attribute to a class that extendsDoctrine\DBAL\Types\Type. If you provide anameargument to the attribute, that name will be used as the type identifier in Doctrine. If no name is provided, the class name will be used as the default.namespace App\Doctrine\Type; use Doctrine\Bundle\DoctrineBundle\Attribute\AsDbalType; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\Type; #[AsDbalType(name: 'money')] class MoneyType extends Type { public function getSQLDeclaration(array $column, AbstractPlatform $platform): string { return $platform->getDecimalTypeDeclarationSQL($column); } public function convertToPHPValue(mixed $value, AbstractPlatform $platform): mixed { return $value; } public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): mixed { return $value; } }Register Doctrine Event Listeners using PHP Attributes
3.3.xStarting with Doctrine Bundle 2.8, you can use the
#[AsDoctrineListener]attribute to automatically register a service as an event listener. This is the recommended way to register listeners in modern Symfony applications.Arguments for
#[AsDoctrineListener]:event(string): The name of the event to listen to (e.g.,'postPersist'). This is the only required argument.priority(int, optional): Defines the execution order. Higher numbers are run earlier. Default is0.connection(string, optional): Restricts the listener to a specific Doctrine connection name (e.g.,'default').
// src/App/EventListener/SearchIndexer.php namespace App\EventListener; use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener; use Doctrine\ORM\Event\LifecycleEventArgs; #[AsDoctrineListener('postPersist'/*, 500, 'default'*/)] class SearchIndexer { public function postPersist(LifecycleEventArgs $event): void { // ... } }Migrate DoctrineOrmMappingsPass from 3.1 to 3.2
3.3.xWhen upgrading from version 3.1 to 3.2, note that the$aliasMapargument for theDoctrineOrmMappingsPassclass and its associated methods has been removed. Namespace aliases are no longer supported in Doctrine.Configure Oracle DB session environment via middleware
3.3.xIf your Oracle DB environment format does not meet Doctrine requirements, you can use a middleware to ensure Doctrine is aware of the correct format. Register the
Doctrine\DBAL\Driver\OCI8\Middleware\InitializeSessionclass as a service tagged withdoctrine.middlewarefor your specific connection.This middleware sets the following environment variables in the Oracle DB session:
NLS_TIME_FORMAT="HH24:MI:SS"NLS_DATE_FORMAT="YYYY-MM-DD HH24:MI:SS"NLS_TIMESTAMP_FORMAT="YYYY-MM-DD HH24:MI:SS"NLS_TIMESTAMP_TZ_FORMAT="YYYY-MM-DD HH24:MI:SS TZH:TZM"
services: oracle.middleware: class: Doctrine\DBAL\Driver\OCI8\Middleware\InitializeSession tags: - { name: doctrine.middleware, connection: default }Enable DoctrineBundle in older Symfony versions (AppKernel.php)
3.3.xIf your project does not have a
config/bundles.phpfile (indicating an older Symfony version), you must manually register the bundle inapp/AppKernel.phpby adding a new instance ofDoctrine\Bundle\DoctrineBundle\DoctrineBundleto the$bundlesarray within theregisterBundles()method.<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = [ // ... new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(), ]; // ... } // ... }