I want to create ApiResource Patch reqeust withouth identifier using custom persistor. And update only one of UserProfileApi fields (firstName
or lastName
) independently.
#[ApiResource(
uriTemplate: '/user/profile.{_format}',
// uriTemplate: '/user/profile/{id}.{_format}',
operations: [
new Patch(
provider: CustomItemStateProvider::class,
processor: CustomEntityStateProcessor::class,
),
],
stateOptions: new Options(entityClass: User::class),
)]
class UserProfileApi implements ApiResourceInterface
{
#[ApiProperty(readable: false, writable: false, identifier: true)]
public ?int $id = null;
public ?string $firstName = null;
public ?string $lastName = null;
}
Patch request body:
{"firstName":"John"}
If I am using identifier {id}
in uriTemplate
:uriTemplate: '/user/profile/{id}.{_format}'
– all works fine.
I find, that CustomItemStateProvider
is called and User Entity from Doctrine is provided.
readonly class CustomEntityStateProcessor implements ProviderInterface
{
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$entity = $this->itemProvider->provide($operation, $uriVariables, $context);
dump($entity)
...
And somehow in CustomEntityStateProcessor
i am getting UserProfileApi ALREDY with updated firstName
, and lastName
is from database.
readonly class CustomEntityStateProcessor implements ProcessorInterface
{
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): ?ApiResourceInterface
{
dump($data)
...
BUT IF I REMOVE {id}
from uriTemplate
uriTemplate: '/user/profile
CustomItemStateProvider
is NOT CALLED, and IN CustomEntityStateProcessor
I AM GETTING UserProfileApi object with "firstName":"John"
and "lastName":null
. AND CustomEntityStateProcessor
on persit try to create new user.
HOW I CAN PASS UserProfileApi request data and Entity data to CustomEntityStateProcessor to update in Entity only firstName, and all the rest entity data leave the same?