Symfony 8.2 already introduced encrypted Doctrine columns. This article showcases other improvements of the Doctrine integration, most of them aimed at removing configuration that you had to write by hand.

Discovering Entities Automatically

Julien Robic
Contributed by Julien Robic in #65437

Doctrine needs to know where your entities are, which is why applications define one or more mappings in their configuration. This is repetitive in applications that split their entities across many directories, such as one per bounded context.

Since Symfony 7.3, the service container already finds those classes when it scans the src/ directory, in order to exclude them from the container. In Symfony 8.2, the classes that define the #[Entity] or #[MappedSuperclass] attributes also get a new doctrine.orm.entity tag. DoctrineBundle 3.4 uses that tag to register your entities, so you no longer need to tell it where they are:

1
2
3
4
5
6
7
8
9
10
# config/packages/doctrine.yaml
  doctrine:
      orm:
-         auto_mapping: true
-         mappings:
-             App:
-                 type: attribute
-                 is_bundle: false
-                 dir: '%kernel.project_dir%/src/Entity'
-                 prefix: 'App\Entity'

Discovery is enabled when the entity manager defines neither mappings nor auto_mapping. If you need to keep any of them (e.g. auto_mapping for the entities provided by bundles), set the auto_discover_entities option to true; explicit mappings take precedence over discovered entities. Discovery requires Doctrine ORM 3.6 or higher.

Caution

Only the classes scanned by the container are discovered. If your services.yaml file still excludes the entity directory (e.g. exclude: '../src/Entity/'), remove that line.

No Explicit Type for DatePoint Columns

Wojciech Kania
Contributed by Wojciech Kania in #63772

The Clock component provides a DatePoint class and the Doctrine bridge provides a date_point type to store it in the database. However, Doctrine could not guess that type from the property type, so you had to repeat it in each column. In Symfony 8.2, that is no longer needed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// src/Entity/Invoice.php
  namespace App\Entity;

  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Clock\DatePoint;

  #[ORM\Entity]
  class Invoice
  {
      // ...

-     #[ORM\Column(type: 'date_point')]
+     #[ORM\Column]
      public DatePoint $issuedAt;
  }

This works in all entity managers. If you configured your own typed field mapper in DoctrineBundle, it is still asked first. The day_point and time_point types also store DatePoint objects, so you still have to set them explicitly.

Denormalizing into Doctrine Collections

Jérôme Tamarelle
Contributed by Jérôme Tamarelle in #66144

When a property was typed as a collection class, the Serializer component denormalized its elements correctly, but then passed them as a plain array. Constructors, setters and properties typed with the collection rejected that array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace App\Dto;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

final readonly class Team
{
    public function __construct(
        public string $name,
        /** @var Collection<int, Member> */
        public Collection $members = new ArrayCollection(),
    ) {
    }
}

In Symfony 8.2, the Serializer keeps the declared collection class:

1
2
3
4
5
6
$team = $serializer->denormalize([
    'name' => 'Core Team',
    'members' => [['name' => 'Jane'], ['name' => 'John']],
], Team::class);

// $team->members is an ArrayCollection of Member objects

A concrete class such as ArrayCollection<int, Member> is created by the Serializer itself. The Collection interface is handled by a new CollectionDenormalizer in the Doctrine bridge, which creates an ArrayCollection by default. Nothing changes when the target accepts an array (an array type, an untyped property or an adder method): it still receives an array. This also works when mapping request data with #[MapRequestPayload].

DBAL-Based Messenger Middleware

Mathieu Lechat Nicolas Grekas
Contributed by Mathieu Lechat and Nicolas Grekas in #65302 , #65329 and #65346

The Doctrine bridge provides several Messenger middleware to ping and close database connections, to wrap handlers in a transaction, etc. All of them worked through an entity manager, so you could not use them in applications that use Doctrine DBAL without the ORM.

Symfony 8.2 adds four middleware that work on DBAL connections instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
# config/packages/messenger.yaml
framework:
    messenger:
        buses:
            messenger.bus.default:
                middleware:
                    # no arguments: applies to all DBAL connections
                    - doctrine_dbal_ping_connection
                    - doctrine_dbal_open_transaction_logger
                    # only the given connection
                    - doctrine_dbal_close_connection: ['reporting']
                    # a single connection (the default one if not set)
                    - doctrine_dbal_transaction: ['reporting']

These names require DoctrineBundle 3.4 or higher. Except for the transaction middleware, they apply to all DBAL connections when you pass no name. The previous close and transaction logger middleware applied only to the connection of the default entity manager.

Published in #Living on the edge