Custom Filter does not work
See original GitHub issueAPI Platform version(s) affected: 2.6.1
Description
Custom filter is registered, but does nothing.
How to reproduce
<?php
// api/src/Filter/RegexpFilter.php
namespace App\Filter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;
final class IsNotFilter extends AbstractContextAwareFilter
{
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
{
// otherwise filter is applied to order and page as well
if (
!$this->isPropertyEnabled($property, $resourceClass) ||
!$this->isPropertyMapped($property, $resourceClass)
) {
return;
}
$parameterName = $queryNameGenerator->generateParameterName($property); // Generate a unique parameter name to avoid collisions with other filters
$queryBuilder
->andWhere(sprintf('1 != 1', $parameterName, $property))
->setParameter($parameterName, $value);
}
// This function is only used to hook in documentation generators (supported by Swagger and Hydra)
public function getDescription(string $resourceClass): array
{
if (!$this->properties) {
return [];
}
$description = [];
foreach ($this->properties as $property => $strategy) {
$description["is_not_$property"] = [
'property' => $property,
'type' => 'string',
'required' => false,
'swagger' => [
'description' => 'Filter for relations that are not the specific entity.',
'name' => 'Is Not Filter',
'type' => 'Custom Filter',
],
];
}
return $description;
}
}
Additional Context
Filter appears in graphql schema and I can use it in queries. However, it does not affect output in any way. For simplicity, I am using the ‘1 != 1’ condition for the andWhere in the sample code, but I’ve tried all kinds of queries that function fine in Repositories. Also tried to copy the SearchFilter code into a CustomFilter, same result.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9
Top Results From Across the Web
5 Reasons why your Excel filter may not be working
Another reason why your Excel filter may not be working may be due to merged cells. Unmerge any merged cells or so that...
Read more >Custom Filter Not Working - Designer - Alteryx Community
Solved: Hi! I am trying to use a custom filter in the Filter Tool to only include certain data in my workflow. My...
Read more >Solved: Custom Filter not working - Atlassian Community
Hi, My custom filter is not working can someone help? 1 project 2 boards = 'Product campaign' board goes through process and gets...
Read more >Custom filter does not work — DataTables forums
Yes, basically the global search works first then the plugin function processes only those rows left after Datatables has filtered the rows.
Read more >Custom filter in MS Excel 'Does Not Begin with' is not working ...
So to make sure, I tried for other filters like does 'does not end with' does not contain' etc. and it works correctly....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
No, sorry. Never had the need to.
@Vicolondon There are two issues with their example:
$description["is_not_$property"]
, then you need to transform the $property in the filterProperty function like that -$property = str_replace('is_not_', '', $property);
$this->isPropertyMapped($property, $resourceClass)
check always returns false. Not sure, if it is related to the above issue or not.So, to give you a working example:
I hope this helps.