Search code examples
symfonyargumentsentitytype-hinting

Receive all kind of Entity as an Argument in Symfony


I have a method to get a change set of Entity from the unit of work (entity manager) in Symfony and I would like it to receive all Entities (Post, User...) instead of specific Entity.

public function getChanges(Post $event): array
    {
        $uow = $this->entityManager->getUnitOfWork();
        $uow->computeChangeSets();

        return $uow->getEntityChangeSet($event);
    }

Do you have any idea to do it? One solution is getting the object as the argument but I prefer to get only the Symfony Entity objects in the function.


Solution

  • Look for doctrine entity listener.

    https://symfony.com/doc/current/doctrine/events.html#doctrine-entity-listeners

    And do not filter entity inside it, remove this part from the example:

        // if this listener only applies to certain entity types,
        // add some code to check the entity type as early as possible
        if (!$entity instanceof Product) {
            return;
        }