question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Override DELETE operation: update `status` property instead

See original GitHub issue

Hi!

I want to override all entities’ DELETE operation with a custom implementation that sets a new value to a custom status property instead. For example, when I do DELETE /api/projects/3 I want it to set the status property of the Project entity with ID 3 to value disabled, and NOT remove the record from the database. I want to apply this to all my entities - they all implement a base EntityInterface that contains the getter + setter for this status property.

I spent some time trying the options I could think of, with the following results:

  1. Create a custom operation

The problem I ran into is routing: I want to use this for all my entities, but could not figure out how to have a dynamic route in the action, e.g.:

class SafeDeleteEntity
{
    /**
     * @Route(
     *     name="safe_delete_entity",
     *     path="/{entity}/{id}",
     *     defaults={"_api_resource_class"=EntityInterface::class, "_api_item_operation_name"="safe_delete_entity"}
     * )
     * @Method("DELETE")
     */
    public function __invoke($data) { ... }
}

Usage in entity:

/**
 * @ORM\Entity
 * @ApiResource(
 *     itemOperations={
 *          "delete"={"method"="DELETE", "route_name"="safe_delete_entity"}
 *     }
 * )
 */
class MyEntity implements EntityInterface { ... }

This would result in a 405 error: method DELETE not allowed.

  1. Hook into the event system

I was able to hook into the PRE_WRITE kernel event and check the entity and operation. I understand this is where I can update the entity status property, but I could not figure out how to prevent the DELETE operation. Is this even possible this way?

  1. Create a custom extension

When printing the query builder, it was for the SELECT operation, not DELETE. It sounds logical that we first want to check whether there is an entity with the requested ID to provide the user with a 404 error when it does not. However, if this is not the DELETE operation, then it appears I am not able to override it this way…

Can you please advise me on the correct way to implement this?

Thanks!

Abel Cooking Fox

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
soyukacommented, Apr 28, 2017

Hmm, why are you not using a Doctrine subscriber for this? It looks like you need something like the SoftDeletable extension.

0reactions
soyukacommented, Sep 4, 2017

@toulemol Not really because on the API side you want to DELETE. The thing is that sometimes DELETE on the API doesn’t mean DELETE data but instead “flag this data as deleted”, especially if you need to keep relations.

Closing as old, feel free to open a new one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Override a model's DELETE method from real delete to setting ...
And the DELETE method only change the property to "delete=1" rather than real delete. Is it do-able when using django-rest-framework? And how?
Read more >
How to Implement a Soft Delete with Spring JPA - Baeldung
Soft delete performs an update process to mark some data as deleted instead of physically deleting it from a table in the database....
Read more >
Trigger flows when a row is added, modified, or deleted
This property applies to the Update condition only. Create and Delete apply to all columns of a row. This property in not supported...
Read more >
UpdateItem - Amazon DynamoDB - AWS Documentation
Operation was rejected because there is an ongoing transaction for the item. HTTP Status Code: 400. Examples. Conditional Update. This example updates the ......
Read more >
Updating with Cypher - Developer Guides - Neo4j
Create, Update, and Delete Operations. In the last guide, we learned how to represent nodes, relationships, labels, properties, and patterns in Cypher for ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found