Skip to content

Attribute "AsEntityListener" is missing the "entity" argument #1438

@SimonMarx

Description

@SimonMarx

Hey,

currently im switching everything to attributes in my project. I have some EntityListeners and want to use the AsEntityListener attribute for it.

i would expect that i can it assign like:

#[AsEntityListener(event: Events::prePersist, entity: AuthToken::class)]
class AuthTokenPrePersistListener
{
    public function __invoke(AuthToken $token, LifecycleEventArgs $args): void
    {
        // do stuff
    }
}

But checking the AsEntityListener class shows that i cannot assign a entity class to that listener attribute

class AsEntityListener
{
    public function __construct(
        public ?string $event = null,
        public ?string $method = null,
        public ?bool $lazy = null,
        public ?string $entityManager = null
    ) {
    }
}

Shouldn't it be possible by adding public ?string entity = null argument to the Attribute construct and then extends the part where the Autoconfiguration is configured in the DoctrineExtension?

            $container->registerAttributeForAutoconfiguration(AsEntityListener::class, static function (ChildDefinition $definition, AsEntityListener $attribute) {
                $definition->addTag('doctrine.orm.entity_listener', [
                    'event'          => $attribute->event,
                    'method'         => $attribute->method,
                    'lazy'           => $attribute->lazy,
                    'entity_manager' => $attribute->entityManager,
                    'entity'         => $attribute->entity      // extended to add entity as attribute
                ]);
            });

Am i wrong? Do i miss some part of code where is described what to do when using the AsEntityListener to assign it explicit to a entity class?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions