Filter 有没有办法用DTO向自定义端点添加过滤器?

Filter 有没有办法用DTO向自定义端点添加过滤器?,filter,dto,api-platform.com,Filter,Dto,Api Platform.com,我有一个自定义端点(它执行一些自定义聚合),该端点的返回是DTO的集合。我想为api的消费者添加一些过滤器建议。这可能吗?你怎么能做到 总而言之: 我有一个DTO(ApiResource,但未链接到条令或数据库) 我有一个自定义GET端点,它返回DTO集合(过滤或不过滤) 我想将筛选器sugestion添加到此端点 我是否应该以某种方式修改hydra:搜索 我试图在我的DTO上添加ApiFilters(就像我对entites所做的那样),但是ApiFilters链接到了doctrine,所以

我有一个自定义端点(它执行一些自定义聚合),该端点的返回是DTO的集合。我想为api的消费者添加一些过滤器建议。这可能吗?你怎么能做到

总而言之:

  • 我有一个DTO(ApiResource,但未链接到条令或数据库)
  • 我有一个自定义GET端点,它返回DTO集合(过滤或不过滤)
  • 我想将筛选器sugestion添加到此端点
我是否应该以某种方式修改hydra:搜索


我试图在我的DTO上添加ApiFilters(就像我对entites所做的那样),但是ApiFilters链接到了doctrine,所以它给了我以下错误:
供应商/api平台/core/src/Bridge/doctrine/Common/propertyHelperRait.php上调用成员函数getClassMetadata(),为了解决这个问题,我创建了一个没有
$this->isPropertyMapped
部分的自定义过滤器

我将
apipplatform\Core\Bridge\doctor\Orm\Extension\FilterExtension
注入到我的集合提供程序中,并使用
$this->filterExtension->applyToCollection($qb、$queryNameGenerator、$resourceClass、$operationName、$context)以更改查询

然后我只需要在dto对象中配置自定义过滤器

@ApiFilter(SearchFilter::class,properties={“columnName”:“exact”})


当我为我的报告第9章编写报告时,我也有这个想法。最简单的解决方案是将操作移动到实体资源,并将其输出设置为DTO类。然后,它可以使用实体类上定义的ApiFilters。控制器将获取实体作为其输入数据。
<?php

declare(strict_types=1);

namespace App\ThirdParty\ApiPlatform\Filter;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;

final class SearchFilter extends AbstractContextAwareFilter
{

    protected function filterProperty(
        string $property,
        $value,
        QueryBuilder $queryBuilder,
        QueryNameGeneratorInterface $queryNameGenerator,
        string $resourceClass,
        string $operationName = null
    ): void {
        if (
            !$this->isPropertyEnabled($property, $resourceClass)
        ) {
            return;
        }

        $parameterName = $queryNameGenerator->generateParameterName($property);

        $rootAlias = $queryBuilder->getRootAliases()[0];

        $queryBuilder
            ->andWhere(sprintf('%s.%s = :%s', $rootAlias, $property, $parameterName))
            ->setParameter($parameterName, $value);
    }

    public function getDescription(string $resourceClass): array
    {
        if (!$this->properties) {
            return [];
        }

        $description = [];
        foreach ($this->properties as $property => $strategy) {
            $description["regexp_$property"] = [
                'property' => $property,
                'type' => 'string',
                'required' => false,
                'swagger' => [
                    'description' => 'description',
                    'name' => $property,
                    'type' => 'type',
                ],
            ];
        }

        return $description;
    }
}