0
votes

I'm using api-platform with Elasticsearch as the datasource. Using the filters you specify the object property that you want to filter. This is then exposed as a query parameter on the api.

Is there a way to map a different name for the query parameter?

So instead of exposing the model property name of "updatedDate" you could instead just use "date"?

e.g. GET /products?updatedDate=today becomes GET /products?date=today but underneath it still filters on updatedDate?

1

1 Answers

1
votes

You can extend a filter that you use. For example, if you use DateFilter for updatedDate property, your class can look like this:

namespace App\Filter;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;

final class UpdatedDateFilter extends DateFilter
{

    /** @param mixed $values */
    protected function filterProperty(string $property, $values, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null): void
    {
        if ($property === 'date') {
            $property = 'updatedDate';
        }

        parent::filterProperty($property, $values, $queryBuilder, $queryNameGenerator, $resourceClass, $operationName)
    }
}

And use it for your entity:

use App\Filter\UpdatedDateFilter;

/**
* @ApiFilter(UpdatedDateFilter::class, properties={"date"})
*/
class Product 
{ .... }