api-platform / api-platform/core

Mercure DELETE updates not triggered when using SoftDeleteable entities

未關閉
#8,026 0 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視
doctrine Serializer
主要語言
PHP
星號
2.6k
分支
980
平均合併
2 天 4 小時
30 天內合併 PR
49

描述

Hello there,

For the records, I noticed that the Mercure updates are not dispatched anymore when using [Gedmo SoftDeleteable](https://github.com/doctrine-extensions/DoctrineExtensions/blob/main/doc/softdeleteable.md) during DELETE operations.

At first I thought that at least an "update" event would be fired (I mean, a Mercure update containing the whole entity instead of just its IRI) but it wasn't the case. This is because the SoftDeleteable extension already hooks on Doctrine to move the entity from scheduled deletions to scheduled updates, but Mercure Bundle cannot be aware of it, as the entity lands in a special `UnitOfWork::$extraUpdates` property which has no accessors.

Since it looks like sort of a listener priority issue, I first tried putting `MercureBundle::class` above `StofDoctrineExtensionsBundle::class` in `bundles.php` - but it didn't help either, probably because Mercure bundle uses an Event Subscriber, whereas the SoftDeleteable extension uses an Event Listener.

So the only way I figured out to fix this was to set a higher priority to the Mercure bundle's subscriber. I could not make it through my own `services.yaml` because of some random errors, hence I used a compiler pass instead:
```php
declare(strict_types=1);

namespace App\Doctrine;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

use function array_column;
use function array_combine;
use function array_values;

final class ChangeMercureListenerPriority implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
$publisher = $container->findDefinition('api_platform.doctrine.orm.listener.mercure.publish');
$tags = $publisher->getTags();
$tags['doctrine.event_listener'] = array_combine(
array_column($tags['doctrine.event_listener'], 'event'),
$tags['doctrine.event_listener'],
);
$tags['doctrine.event_listener']['onFlush']['priority'] = 128;
$tags['doctrine.event_listener'] = array_values($tags['doctrine.event_listener']);
$publisher->setTags($tags);
}
}
```

Then I register it into my `App\Kernel::prepareContainer()` (for some reason it didn't work from `App\Kernel::build()`).

I don't know if this is something that you guys would like to fix or improve, otherwise feel free to close this so that it can help people struggling with the same issue.

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。