api-platform / api-platform/core

PUT queries on a Resource with a composite key fail with a duplicate key error

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

描述

**API Platform version(s) affected**:
3.2.16

**Description**
After an update in the latest version of ApiPlatform (from v2.7.x)
Each PUT calls on Resources with composite keys fail with a duplicate error.

It seems that the detection of the existing object fails and so the doctrine `flush()` try to create the resource rather than updated it.

The limitation seems known (https://github.com/api-platform/core/blob/5523bf5df93783582bed5591ff35c71e0942a978/src/Doctrine/Common/State/PersistProcessor.php#L58) but I prefer to create an issue so we can follow the resolution.

**How to reproduce**
* create a Resource (also Doctrine entity) with a composite Id
* POST a new resource
* Set a PUT on this resource
* The error is triggered

**Possible Solution**

Awaiting a resolution I have added a specific Processor as workaround

```

*/
class CompositeIdentifierPutProcessor implements ProcessorInterface
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
) {
}

/**
* @throws Exception
*/
public function process(
mixed $data,
Operation $operation,
array $uriVariables = [],
array $context = []
): EntityInterface {

if (!$operation instanceof Put) {
throw new Exception('Invalid Operation');
}

if (isset($uriVariables['id']['identifiers']) && count($uriVariables['id']['identifiers']) < 2) {
throw new Exception('This processor should only be used for a Resource with composite identifier');
}

$this->entityManager->flush();
return $data;
}

}
```

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

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

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