symfony API平台:自定义筛选器不工作

cigdeys3  于 2023-10-23  发布在  其他
关注(0)|答案(1)|浏览(86)

我创建了一个自定义过滤器,以检查表属性是否不是“Brouillon”,但当我在Entity中调用它时,什么也没有发生。
以下是我的档案:
NotCaracteristiqueFilter.php:

<?php
// api/src/Filter/RegexpFilter.php

namespace App\Filter;

use Doctrine\ORM\QueryBuilder;
use ApiPlatform\Metadata\Operation;
use Symfony\Component\PropertyInfo\Type;
use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;

final class NotCaracteristiqueFilter extends BooleanFilter
{
    public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?string $operationName = null, array $context = [])    {

            $value = $this->extractProperties([$this->getProperties()], $queryBuilder);

            if ($value === true) {
                $rootAlias = $queryBuilder->getRootAliases()[0];
                $queryBuilder->andWhere(sprintf('%s.etuCaracteristique != :brouillon', $rootAlias));
                $queryBuilder->setParameter('brouillon', 'Brouillon');
            }

    }

}

service.yaml:

# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'
            - '../src/EventListener/'

    App\Filter\NotCaracteristiqueFilter:
        
    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

Etude.php(我的实体):

<?php

namespace App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use App\Filter\NotCaracteristiqueFilter;
use ApiPlatform\Core\Annotation\ApiFilter;
use Doctrine\Common\Collections\Collection;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;

/**
 * Etude
 * @ApiResource(
 *      collectionOperations={"get","post"},
 *      itemOperations={
 *      "get"={
 *          "normalization_context" ={"groups"= {"client", "test", "sponsor", "commercial", "user", "projet"}}
 * 
 *      }, 
 *      "patch","delete"},
 *      collectionOperations={
 *          "get"={
 *              "normalization_context" ={"groups"= {"client", "test", "sponsor", "commercial", "user", "projet"}}
 *          }
 *      },
 *     filters={NotCaracteristiqueFilter::class}
 * )
 * @ApiFilter(SearchFilter::class, properties={"etuNum": "start", "etuCaracteristique" : "partial", "etuDe.paraphe" : "partial", "etuSponsor.spoIntitule" : "partial","etuCommercial.comNom": "partial","etuStudyManager.paraphe":"partial","etuStatus":"partial"})}) 
 * @ORM\Table(name="etude", indexes= {@ORM\Index(name="Etu_Commerical", columns={"Etu_Commercial"}), @ORM\Index(name="Etu_Test", columns={"Etu_Test"}), @ORM\Index(name="Etu_client", columns={"Etu_Cli"}), @ORM\Index(name="Etu_Sponsor", columns={"Etu_Sponsor"})})
 * 
 * @ORM\Entity
 */
class Etude
{
...
}

我已经检查了API Platform的文档,以配置自定义过滤器,我检查了互联网上的其他来源,但我发现没有任何看起来像我的情况下,即使在堆栈溢出。我也清除了我的缓存,检查我的Symphony日志,但没有问题一直显示。Ib还试图错误配置我的过滤器,以检查它是否甚至检测到,它显示了一个错误,这让我认为我的过滤器被检测到。

0s7z1bwu

0s7z1bwu1#

我假设你使用的API平台版本低于2.7.
您的自定义过滤器需要扩展AbstractContextAwareFilter,参见API平台自定义过滤器文档(2.5版):
可以通过实现ApiPlatform\Core\Api\FilterInterface接口来编写自定义过滤器
对于你的实体,你必须这样做(不要忘记添加使用NotCharacteristiqueFilter):

use App\Filter\NotCaracteristiqueFilter;

/**
 * @ApiResource(
 *  ...
 * )
 * @ApiFilter(NotCaracteristiqueFilter::class)
 */
class Etude
{
    // ...
}

您不需要在@ApiResource中添加filters={NotCaracteristiqueFilter::class},并且由于自动装配,您不需要更新services.yaml。还要注意,您在@ApiResource中有一个重复的collectionOperations密钥。

相关问题