Skip to content
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

Custom filters documentation #1645

Open
BrandonGillis opened this issue Nov 8, 2022 · 0 comments
Open

Custom filters documentation #1645

BrandonGillis opened this issue Nov 8, 2022 · 0 comments

Comments

@BrandonGillis
Copy link

BrandonGillis commented Nov 8, 2022

When looking at the custom filters documentation there's multiple example of custom filters, but with different "alias".
There's this first example with o used as an hardcoded alias (REGEXP(o.%s, :%s)).

    protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, Operation $operation = null, array $context = [])
    {
        // 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('REGEXP(o.%s, :%s) = 1', $property, $parameterName))
            ->setParameter($parameterName, $value);
    }

Others example use the rootAliases :

//App/Filter/CustomAndFilter.php
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, Operation $operation = null, array $context = []) {  
  $rootAlias = $queryBuilder->getRootAliases()[0];  
  foreach (array_keys($this->getProperties()) as $prop) { //NOTE: we use array_keys because getProperties() returns a map of property => strategy
      if (!$this->isPropertyEnabled($prop, $resourceClass) || !$this->isPropertyMapped($prop, $resourceClass)) {  
          return;  
      }  
      $parameterName = $queryNameGenerator->generateParameterName($prop);  
      $queryBuilder  
          ->andWhere(sprintf('%s.%s LIKE :%s', $rootAlias, $prop, $parameterName))  
          ->setParameter($parameterName, "%" . $value . "%");  
  }  
}

Is there any reason that o was used as an alias in the first example ? What could it do if I use the same hardcoded alias (o) in my project ?

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

No branches or pull requests

1 participant