gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:47:37 24 4
gpt4 key购买 nike

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

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

我使用的是软删除过滤器,因此实体实际上不在 $uow->getScheduledEntityDeletions() 中,而是在 $uow->extraUpdates 中code> 标记已删除的标志已更改。

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

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);
}

}

最佳答案

因此,这个问题的详细答案如下:

监听 preSoftDelete 事件 ( Symfony2 DoctrineExtensions preSoftDelete event call ):

    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);
}
}

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

关于symfony - 从 Doctrine UnitOfWork 获取预定的额外更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22957882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com