Home > Software engineering >  How to override the PUT operation update process in the Api Platform
How to override the PUT operation update process in the Api Platform

Time:11-12

I'm trying to override the PUT operation to perform my actions under certain conditions. That is, if the sent object is different from the original object (from the database), then I need to create a new object and return it without changing the original object.

Now when I execute the query I get a new object, as expected, but the problem is that the original object also changes

Entity

#[ApiResource(
    operations: [
        new Get(),
        new GetCollection(),
        new Post(controller: CreateAction::class),
        new Put(processor: EntityStateProcessor::class),
    ],
    paginationEnabled: false
)]
class Entity

EntityStateProcessor

final class PageStateProcessor implements ProcessorInterface
{

    private ProcessorInterface $decorated;
    private EntityCompare $entityCompare;
 
    public function __construct(ProcessorInterface $decorated,  EntityCompare $entityCompare)
    {
        $this->decorated = $decorated;
        $this->entityCompare = $entityCompare;
    } 
    
    public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
    {
        if (($this->entityCompare)($data)) { // checking for object changes
            $new_entity = clone $data; // (without id)
            // do something with new entity
            return $this->decorated->process($new_entity, $operation, $uriVariables, $context);
        }
        return  $data;
    }  
    
}

I don't understand why this happens, so I return a clone of the original object to the process. It would be great if someone could tell me what my mistake is.


I also tried the following before returning the process

  1. $this->entityManager->refresh($data); - Here I assumed that the original instance of the object will be updated with data from the database and the object will not be updated with data from the query
  2. $this->entityManager->getUnitOfWork()->detach($data); - Here I assumed that the object would cease to be manageable and would not be updated

But in both cases the state of the original $data changes.


I'm using ApiPlatform 3.0.2

CodePudding user response:

The error is that the main entity is related to an additional entity, so it's not enough to detach the main entity from UnitOfWork. So use the Doctrine\ORM\UnitOfWork->clear(YourEntity::class) method to detach all instances of the entity, and you do the same for relationships.

Once the entity is detach, cloning the entity becomes pointless because the previous entity instance isn't managed by the Doctrine ORM, so my code rearranges itself like this:

public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
{
    if (($this->entityCompare)($data)) { // checking for object changes
        $this->getEntityManager()->getUnitOfWork()->clear(Entity::class);
        $this->getEntityManager()->getUnitOfWork()->clear(EelatedEntity::class);
        // do something with new entity
        return $this->decorated->process($data, $operation, $uriVariables, $context);
    }
    return  $data;
}
  • Related