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.

Custom Filter does not work

See original GitHub issue

API 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:open
  • Created 2 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
plamenhcommented, Nov 25, 2021

No, sorry. Never had the need to.

1reaction
plamenhcommented, Nov 25, 2021

@Vicolondon There are two issues with their example:

  1. First, the property name is not properly passed to the filterProperty function. Basically, if you name your filter like this - $description["is_not_$property"], then you need to transform the $property in the filterProperty function like that - $property = str_replace('is_not_', '', $property);
  2. The $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:

<?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
    ) {
        $property = str_replace('is_not_', '', $property);

        // otherwise filter is applied to order and page as well
        if (
            !$this->isPropertyEnabled($property, $resourceClass) 
            // || !$this->isPropertyMapped($property, $resourceClass)
            || $value === 0
        ) {
            return;
        }


        $parameterName = $queryNameGenerator->generateParameterName($property); // Generate a unique parameter name to avoid collisions with other filters

        // throw new \Exception($parameterName);

        $queryBuilder
            ->andWhere(sprintf('o.%s != :%s OR o.%s is NULL', $property, $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' => 'int',
                'required' => false,
                'swagger' => [
                    'description' => 'Filter for relations that are not the specific entity.',
                    'name' => 'Is Not Filter',
                    'type' => 'Custom Filter',
                ],
            ];
            // throw new \Exception(print_r($description));
        }

        return $description;
    }
}

I hope this helps.

Read more comments on GitHub >

github_iconTop 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 >

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