Creating/Persisting Entity on create/update/change of another one in Doctrine

I am working on a Symfony (5.1) application using Doctrine 2.7.
I want to create logging-Entities for another entity that I have.
So I got some Change-Entity and for each change in my original entities I want to create a new Change-instance and persist it.

So I thought I might use doctrine’s prePersist, postUpdate annotations and do on basically doing something like this:

 * @ORMEntity
 * @ORMHasLifecycleCallbacks()
class OriginalEntity
     * @ORMPrePersist
    public function onCreate(LifecycleEventArgs $args)
        $em = $args->getEntityManager();
        //...create a Change entity with some Information and persist it...

But it has been stated in multiple threads here on SO that it is bad practice to use the entity manager inside entity-classes.

So how would I do that in a "better-practice" way?

This seems to be a common task after all.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *