Everything works like a charm except for the PUT operation. I get the 500 error because Doctrine can not retrieve the entity identifier. Please see the details below.
How to reproduce
Configure an ApiResource over DTO with stateOptions settings based on a Doctrine Entity.
Perform the PUT request on the ApiResource
ER: PUT works as expected.
AR:
{
"title": "An error occurred",
"detail": "Given object is not an instance of the class this property was declared in",
"status": 500,
"type": "/errors/500",
"trace": [
{
"file": "/home/www/vendor/doctrine/persistence/src/Persistence/Reflection/TypedNoDefaultReflectionPropertyBase.php",
"line": 33,
"function": "isInitialized",
"class": "ReflectionProperty",
"type": "->"
},
{
"file": "/home/www/vendor/doctrine/orm/src/Mapping/ClassMetadata.php",
"line": 616,
"function": "getValue",
"class": "Doctrine\\Persistence\\Reflection\\TypedNoDefaultReflectionProperty",
"type": "->"
},
{
"file": "/home/www/vendor/api-platform/core/src/Doctrine/Common/State/PersistProcessor.php",
"line": 61,
"function": "getIdentifierValues",
"class": "Doctrine\\ORM\\Mapping\\ClassMetadata",
"type": "->"
},
{
"file": "/home/www/src/ApiPlatform/State/Processor/Doctrine/EntityClassDtoStateProcessor.php",
"line": 32,
"function": "process",
"class": "ApiPlatform\\Doctrine\\Common\\State\\PersistProcessor",
"type": "->"
},
Possible Solution
I've debugged it a little and it seems Doctrine tries to fetch an identifier from $context['previous_data'] which is a DTO and not an entity, so it fails.
My understanding is that the case with stateOptions configuration should be handled inside the \ApiPlatform\Doctrine\Common\State\PersistProcessor.
Please assist with a correct solution here, or point me to my mistake. Thank you!
The mapper should transform your dto into an entity, you may need to set $context['previous_data'] = $entity = $this->microMapper->map($context['previous_data'], $entityClass);?
Hello! Hope I find you in a good mood!
API Platform version(s) affected: 3.3.7
Description
I followed the next guides to create multiple ApiResources over one entity using DTO and stateOptions. https://www.youtube.com/watch?v=IVJjADhU7WM https://symfonycasts.com/screencast/api-platform-extending
Everything works like a charm except for the PUT operation. I get the 500 error because Doctrine can not retrieve the entity identifier. Please see the details below.
How to reproduce
ER: PUT works as expected. AR:
Possible Solution
I've debugged it a little and it seems Doctrine tries to fetch an identifier from
$context['previous_data']
which is a DTO and not an entity, so it fails.My understanding is that the case with stateOptions configuration should be handled inside the
\ApiPlatform\Doctrine\Common\State\PersistProcessor
.Please assist with a correct solution here, or point me to my mistake. Thank you!
Additional Context
My ApiResource
My Processor