Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable attaching an entity listener without specifying an event #1560

Merged

Conversation

HypeMC
Copy link
Contributor

@HypeMC HypeMC commented Oct 11, 2022

Requires doctrine/orm#10122

Currently, when registering an entity listener the event name needs to be specified, eg:

use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Events;

#[AsEntityListener(event: Events::prePersist, entity: MyEntity::class)]
final class MyEntityListener
{}

If the event is not specified, it's not registered. This is because of the following check:

if (isset($attributes['entity']) && isset($attributes['event'])) {
$this->attachToListener($container, $name, $this->getConcreteDefinitionClass($container->findDefinition($id), $container, $id), $attributes);
}

However, when the EntityListeners attribute is used the events get read from the listener class by matching the method names to the names of possible events, eg:

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\EntityListeners([MyEntityListener::class])]
class MyEntity
{}

The following code is triggers this behavior:

https://github.com/doctrine/orm/blob/06c77cebb5aa7ee3cb57f8cc0d2224538de154d7/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php#L527-L529

The problem with this approach is that it's not very intuitive since the configuration is split across multiple files making it hard to use.

With this PR, the events will attempt to be auto-mapped in the AttachEntityListenersListener if null is passed as the event name (see doctrine/orm#10122), eg:

use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;

#[AsEntityListener(entity: MyEntity::class)]
final class MyEntityListener
{}

@ostrolucky ostrolucky force-pushed the 2.7.x branch 3 times, most recently from 1f11ec2 to bba6634 Compare October 16, 2022 09:24
@ostrolucky ostrolucky changed the base branch from 2.7.x to 2.8.x October 16, 2022 09:42
@ostrolucky
Copy link
Member

thx!

@ostrolucky ostrolucky merged commit e781f9c into doctrine:2.8.x Oct 16, 2022
@ostrolucky ostrolucky added this to the 2.8.0 milestone Oct 16, 2022
@HypeMC HypeMC deleted the attach-entity-listener-with-no-event branch October 16, 2022 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants