Persisting other entities inside preUpdate of Doctrine Entity Listener

后端 未结 4 1971
忘掉有多难
忘掉有多难 2020-12-09 16:44

For clarity I continue here the discussion started here.

Inside a Doctrine Entity Listener, in the preUpdate method (where I have access to both the old and new val

4条回答
  •  误落风尘
    2020-12-09 16:56

    Don't use preUpdate, use onFlush - this allows you to access the UnitOfWork API & you can then persist entities.

    E.g. (this is how I do it in 2.3, might be changed in newer versions)

        $this->getEntityManager()->persist($entity);
        $metaData = $this->getEntityManager()->getClassMetadata($className);
        $this->getUnitOfWork()->computeChangeSet($metaData, $entity);
    

提交回复
热议问题