Skip to content

Purge parent collections in inheritance cases #7034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
CvekCoding opened this issue Mar 20, 2025 · 3 comments
Open

Purge parent collections in inheritance cases #7034

CvekCoding opened this issue Mar 20, 2025 · 3 comments
Labels

Comments

@CvekCoding
Copy link
Contributor

Description
In the case when we clear the cache, we haven't covered the scenario where an entity is part of a class hierarchy (such as STI). In this case, the cache for all parent collections should also be cleared.

For example, we have a Product and its subclass ChildProduct. If we already have a Products collection in the cache, when a new ChildProduct is created, only the cache for the ChildProduct collection will be purged, while the parent collection will remain unchanged.

In my opinion, this is more of a bug than a feature request.

@CvekCoding
Copy link
Contributor Author

CvekCoding commented Mar 20, 2025

For now I created the following processor decorator to work-around the issue:

#[AsDecorator(decorates: 'api_platform.doctrine.orm.state.persist_processor')]
final readonly class PurgeParentCacheCollection implements ProcessorInterface
{
    public function __construct(
        #[Autowire(service: 'api_platform.http_cache.purger')]
        private PurgerInterface $purger,
        private PersistProcessor $persistProcessor,
        private IriConverterInterface $iriConverter,
        private ResourceClassResolverInterface $resourceClassResolver,
        private EntityManagerInterface $entityManager,
    ) {}

    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
    {
        $toReturn = $this->persistProcessor->process($data, $operation, $uriVariables, $context);

        $classMetadata = $this->entityManager->getClassMetadata($data::class);
        if ($classMetadata->isInheritanceTypeNone()) {
            return $toReturn;
        }

        foreach ($classMetadata->parentClasses as $parentClass) {
            if ($this->resourceClassResolver->isResourceClass($parentClass)) {
                $iris[] = $this->iriConverter->getIriFromResource($parentClass, UrlGeneratorInterface::ABS_PATH, new GetCollection());
            }
        }

        if (!empty($iris)) {
            $this->purger->purge($iris);
        }

        return $toReturn;
    }
}

@soyuka soyuka added the bug label Mar 21, 2025
@soyuka
Copy link
Member

soyuka commented Mar 21, 2025

Interesting, we should probably handle this directly inside the PurgeListener, would you be able to patch this? Thanks!

@CvekCoding
Copy link
Contributor Author

CvekCoding commented Mar 21, 2025

Ok I'll try. I think you need tests for this too? @soyuka

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants