symfonydoctrineapi-platform.comsoft-deletedoctrine-extensions

ApiPaltform - recover soft deleted element


I'm using ApiPlatform and Symfony5

I've implemented softdelete on an entity User

This entity is softdeleted properly, I still want to displayed the deleted users for some roles, so I'm trying a DoctrineExtension to modify the request dynamically.

Here is my UserExtension.php:

    public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = []): void
    {
        $this->addWhere($queryBuilder, $resourceClass);
    }

    private function addWhere(QueryBuilder $queryBuilder, string $resourceClass): void
    {

        if (User::class == $resourceClass &&
            $this->security->isGranted(GenericRoles::ROLE_ADMIN)
        ) {
            $rootAlias = $queryBuilder->getRootAliases()[0];

            $queryBuilder->andWhere("$rootAlias.deletedAt > '2019-01-01'");
        }
    }

I verified that I enter in the if condition.

But I still get a 404 on the item I'm trying to recover (which is still in base)

The same request entered in command line in the db return me the item I want to recover.

I've also tried to disable softdelete as follow :

$this->entityManager->getFilters()->disable('softdeleteable');

But doing so disable it for the whole application, and I can't re enable it in the UserExtension

Does anyone know what I'm doing wrong ? Or if there is another way to recover soft deleted items

Or if you got more docs on the subject I'd be gratefull cause I'm runnning out of options

Thanks!


Solution

  • This solution worked for me, I hope it'll work for whoever fall onto this :

    Through a DataProvider it's possbile to disable the filter, query the soft deleted object you want to query and stock it in a variable, then enable the filter again and return your object.

    I don't know if using the a DataProvider this way is a good practice, but it has not created side effect on my application.

    Here is an example:

    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
    {
        if ($this->security->isGranted(GenericRoles::ROLE_ADMIN)) {
            $this->entityManager->getFilters()->disable('softdeleteable');
        }
    
        $softDeletedObject = $this->itemDataProvider->getItem($resourceClass, $id, $operationName, $context);
    
        $this->entityManager->getFilters()->enable('softdeleteable');
    
        return $softDeletedObject;
    }