In the GET operations, I'd like to exclude from the returning collections my entities that have an " archive" field that equals " true ".
I'd like that to be the default for my endpoints like /users or /companies and i want to avoid to add an URL filter by hand like /users?filter[archive]=true
what would be the best way to do that ?
Thanks for any help :)
I had to do something like that, and I solved it by applying a DoctrineExtension to a Collection that will add the WHERE clause to the QueryBuilder.
How?
services:
App\Doctrine\Extension\ArchivedExtension:
tags:
- { name: api_platform.doctrine.orm.query_extension.collection }
You could check if you are receiving a certain "filter" (your "filter[archive]=true" query parameter): if YES, dont apply the condition to the QueryBuilder, your Filter will be applied by the filtering mechanism of ApiPlatform.
Your extension class should looked something like this:
class ArchivedExtension implements QueryCollectionExtensionInterface
{
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = [])
{
$this->addWhere($queryBuilder, $resourceClass, $context);
}
private function addWhere(QueryBuilder $queryBuilder, string $resourceClass, array $context = [])
{
if (MyResource::class !== $resourceClass) {
return;
}
// Search if a "archive" Filter is being requested, if not, apply a restriction to the QueryBuilder
if (array_key_exists('archive', $context['filters'])) {
return;
}
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->andWhere(sprintf('%s.archive = :archive', $rootAlias));
$queryBuilder->setParameter('archive', true);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With