[PR #10786] Fix attach entity listener when reset class metadata factory #12613

Closed
opened 2026-01-22 16:14:37 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/10786

State: closed
Merged: Yes


When resetting entity manager via registry, if we use Doctrine\ORM\Tools\AttachEntityListenersListener to add entity event listeners, it can not be reattached because it delete listener after add on the first time then we will lose all entity listeners after reset entity manager.

Symfony entity listener example:

<?php

declare(strict_types=1);

namespace App\EntityEventListener;


use App\Entity\Company;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;


#[AsEntityListener(event: 'prePersist', entity: Company::class)]
class CompanyListener
{
    public function prePersist() : void
    {
        echo 'It work!';
    }
}

and console command execution:

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $manager = $this->registry->getManager();
        $company = new Company();
        $company->setText('1');
        $manager->persist($company);
        $manager->flush();

        $this->registry->resetManager(); /// there's the problem

        $company = new Company();
        $company->setText('2');
        $manager->persist($company);
        $manager->flush();


        return 0;
    }

Expect It work! should be printed twice but only once.

**Original Pull Request:** https://github.com/doctrine/orm/pull/10786 **State:** closed **Merged:** Yes --- When resetting entity manager via registry, if we use `Doctrine\ORM\Tools\AttachEntityListenersListener` to add entity event listeners, it can not be reattached because it delete listener after add on the first time then we will lose all entity listeners after reset entity manager. Symfony entity listener example: ```php <?php declare(strict_types=1); namespace App\EntityEventListener; use App\Entity\Company; use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener; #[AsEntityListener(event: 'prePersist', entity: Company::class)] class CompanyListener { public function prePersist() : void { echo 'It work!'; } } ``` and console command execution: ```php protected function execute(InputInterface $input, OutputInterface $output): int { $manager = $this->registry->getManager(); $company = new Company(); $company->setText('1'); $manager->persist($company); $manager->flush(); $this->registry->resetManager(); /// there's the problem $company = new Company(); $company->setText('2'); $manager->persist($company); $manager->flush(); return 0; } ``` Expect `It work!` should be printed twice but only once.
admin added the pull-request label 2026-01-22 16:14:37 +01:00
admin closed this issue 2026-01-22 16:14:37 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#12613