gpt4 book ai didi

api-platform.com - 自定义过滤器 API 平台不起作用

转载 作者:行者123 更新时间:2023-12-04 07:23:39 25 4
gpt4 key购买 nike

我正在尝试在 API 平台中实现自定义或过滤器。但由于某种原因,它没有加载。在我的配置下面找到。

这是我的过滤器:

<?php

namespace AppBundle\Filter;

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

use Doctrine\Common\Annotations\AnnotationReader;

final class SearchFilter extends AbstractFilter
{
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
{
if ($property === 'search') {
$this->logger->info('Search for: ' . $value);
} else {
return;
}

$reader = new AnnotationReader();
$annotation = $reader->getClassAnnotation(new \ReflectionClass(new $resourceClass), \AppBundle\Filter\SearchAnnotation::class);

if (!$annotation) {
throw new \HttpInvalidParamException('No Search implemented.');
}

$parameterName = $queryNameGenerator->generateParameterName($property);
$search = [];
$mappedJoins = [];

foreach ($annotation->fields as $field)
{
$joins = explode(".", $field);
for ($lastAlias = 'o', $i = 0, $num = count($joins); $i < $num; $i++) {
$currentAlias = $joins[$i];
if ($i === $num - 1) {
$search[] = "LOWER({$lastAlias}.{$currentAlias}) LIKE LOWER(:{$parameterName})";
} else {
$join = "{$lastAlias}.{$currentAlias}";
if (false === array_search($join, $mappedJoins)) {
$queryBuilder->leftJoin($join, $currentAlias);
$mappedJoins[] = $join;
}
}

$lastAlias = $currentAlias;
}
}

$queryBuilder->andWhere(implode(' OR ', $search));
$queryBuilder->setParameter($parameterName, '%' . $value . '%');
}

/**
* @param string $resourceClass
* @return array
*/
public function getDescription(string $resourceClass): array
{
$reader = new AnnotationReader();
$annotation = $reader->getClassAnnotation(new \ReflectionClass(new $resourceClass), \AppBundle\Filter\SearchAnnotation::class);

$description['search'] = [
'property' => 'search',
'type' => 'string',
'required' => false,
'swagger' => ['description' => 'Filter on ' . implode(', ', $annotation->fields)],
];

return $description;
}
}

在 api_filters.yml 中:

driver.custom_search_filter:
class: 'AppBundle\Filter\SearchFilter'
autowire: true
tags: [ { name: 'api_platform.filter' } ]

在我的注释文件中:

<?php

namespace AppBundle\Filter;

use Doctrine\Common\Annotations\Annotation;
use Doctrine\Common\Annotations\Annotation\Target;
use Doctrine\Common\Annotations\AnnotationException;

/**
* @Annotation
* @Target("CLASS")
*/
final class SearchAnnotation
{
public $fields = [];

/**
* Constructor.
*
* @param array $data Key-value for properties to be defined in this class.
* @throws AnnotationException
*/
public function __construct(array $data)
{
if (!isset($data['value']) || !is_array($data['value'])) {
throw new AnnotationException('Options must be a array of strings.');
}

foreach ($data['value'] as $key => $value) {
if (is_string($value)) {
$this->fields[] = $value;
} else {
throw new AnnotationException('Options must be a array of strings.');
}
}
}
}

最后在我的实体中:

    /**
* A driver that bring meals from hub to customer.
*
*
* @ApiResource(
* attributes={
* "filters"={"driver.search_filter","driver.custom_search_filter"},
* "denormalization_context"={"groups"={"post_driver"}}
* }
* )
* @Searchable({"firstName"})
*
* @ORM\Entity
* @ORM\Table(name="vendor_driver")
*/
class Driver
{

正是根据这里报告的问题:
https://github.com/api-platform/core/issues/398

我没有收到任何错误,但过滤器根本不起作用。我在 Swagger 中看到它。但是当我在 Swagger 中输入一个值时,db 会返回所有实体。它永远不会到达 filterProperty 方法。

有没有人有想法?

最佳答案

我刚刚通过删除 Autowiring 来实现这一点,例如

my.custom_search_filter:
class: AppBundle\Filter\CustomSearchFilter
arguments:
- '@doctrine'
- '@request_stack'
- '@logger'
tags: [ { name: 'api_platform.filter', id: 'search' } ]

希望有帮助。

关于api-platform.com - 自定义过滤器 API 平台不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49372971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com