从 Doctrine UnitOfWork 获取预定的额外更新

问题描述 投票:0回答:2

我有一个 Doctrine 事件监听器来监听 onFlush 事件。我使用它在保存时更新实体上的电子标签。

我需要访问计划删除的实体,以便我可以访问它们的关联对象,但是:

我使用的是软删除过滤器,因此实体实际上不在

$uow->getScheduledEntityDeletions()
中,而是在
$uow->extraUpdates
中,标记已删除的标志已更改。

此变量是私有的,我不知道有任何编程方式可以通知此更改。有什么想法吗?

private function updateIfRequired(OnFlushEventArgs $args)
{
    $em     = $args->getEntityManager();
    $uow    = $em->getUnitOfWork();



    // Entities either updated or just inserted
    $upsertedEntities = array_merge(

        $uow->getScheduledEntityUpdates(),
        $uow->getScheduledEntityInsertions()
    );

    foreach ($upsertedEntities as $entity) {

        if ($entity instanceof ETaggableInterface || $entity instanceof ETagRelatedInterface) {
            $this->updateETag($entity);
        }
    }

    // When soft-deleted, this and getScheduledEntityUpdates are both empty!
    $deletedEntities = $uow->getScheduledEntityDeletions();

    foreach ($deletedEntities as $entity) {

        $this->deletedEntities[spl_object_hash($entity)] = $entity;
        $this->updateETag($entity);
    }

}
php symfony doctrine-orm unit-of-work etag
2个回答
5
投票

所以,这个问题的详细答案是这样的:

监听 preSoftDelete 事件(Symfony2 DoctrineExtensions preSoftDelete 事件调用):

    tags:
        - { name: doctrine.event_listener, event: preSoftDelete }

然后在您的侦听器类中,添加以下方法:

public function preSoftDelete(LifecycleEventArgs $args){
    $entity = $args->getEntity();
    $em = $args->getEntityManager();

    if ($entity instanceof ETaggableInterface || $entity instanceof ETagRelatedInterface) {
        $entity->updateEtag('foo'); // Was not clear what $this->updateEtag() do

        $em->persist($entity);
        $classMetadata = $em->getClassMetadata(get_class($entity));
        $em->getUnitOfWork()->computeChangeSet($classMetadata, $entity);
    }
}

这将更新实体,告诉其持久化,计算更改。


© www.soinside.com 2019 - 2024. All rights reserved.