简体   繁体   中英

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.

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;
    }
if (!$this->entityManager->contains($entity)) {
            throw new Exception('The given object must be doctrine entity');
        } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM