I’m trying to add a custom ‘OR’ SearchFilter for my ApiPlatform Services :
I’m getting a weird error :
The annotation “@AppFilterSearchAnnotation” in class AppEntityfoo does not exist, or could not be auto-loaded.
My Code :
AppFilterOrSearchFilter :
<?php
namespace AppFilter;
use ApiPlatformCoreBridgeDoctrineOrmFilterAbstractContextAwareFilter;
use ApiPlatformCoreBridgeDoctrineOrmUtilQueryNameGeneratorInterface;
use DoctrineORMQueryBuilder;
use DoctrineCommonAnnotationsAnnotationReader;
final class OrSearchFilter extends AbstractContextAwareFilter
{
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), 'AppFilterSearchAnnotation');
if (!$annotation) {
throw new HttpInvalidParamException('No Search implemented.');
}
$parameterName = $queryNameGenerator->generateParameterName($property);
$search = [];
foreach ($annotation->fields as $field)
{
$search[] = "o.{$field} LIKE :{$parameterName}";
}
$queryBuilder->andWhere(implode(' OR ', $search));
$queryBuilder->setParameter($parameterName, '%' . $value . '%');
}
/**
* @param string $resourceClass
* @return array
*/
public function getDescription(string $resourceClass): array
{
$description['search'] = [
'property' => 'search',
'type' => 'string',
'required' => false,
'swagger' => ['description' => 'Searchfilter'],
];
return $description;
}
}
AppFilterSearchAnnotation :
<?php
namespace AppFilter;
use DoctrineCommonAnnotationsAnnotation;
use DoctrineCommonAnnotationsAnnotationTarget;
use DoctrineCommonAnnotationsAnnotationException;
/**
* @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.');
}
}
}
}
services.yaml :
services:
AppFilterOrSearchFilter:
class: AppFilterOrSearchFilter
autowire: true
tags: [ { name: 'api_platform.filter', id: 'search' } ]
AppEntityfoo
namespace AppEntity;
use ApiPlatformCoreAnnotationApiResource;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use ApiPlatformCoreAnnotationApiFilter;
use ApiPlatformCoreBridgeDoctrineOrmFilterSearchFilter;
use DoctrineORMMapping as ORM;
use LocasticApiPlatformTranslationBundleModelAbstractTranslatable;
use LocasticApiPlatformTranslationBundleModelTranslationInterface;
use SymfonyComponentSerializerAnnotationGroups;
use AppFilterSearchAnnotation as Searchable ; //Annotation imported here
/**
* @ApiResource(
* formats={"json"},
* collectionOperations={
* "get" : {"method": "GET"},
* "post" : {
* "method": "POST",
* "normalization_context"={"groups"={"translations"}},
*
* }
* },
* itemOperations={
* "get" : {"method": "GET"},
* "put" : {
* "method": "PUT",
* "normalization_context"={"groups"={"translations"}},
* }
* } ,
* attributes={
* "filters"={"translation.groups","search"},
* "normalization_context"={"groups"={"foo_read"}},
* "denormalization_context"={"groups"={"foo_write"}}
* })
* @ORMEntity(repositoryClass="AppRepositoryfooRepository")
* @Searchable({"id"})
*/
class foo extends AbstractTranslatable
{...}
Composer.json
{
"type": "project",
"license": "proprietary",
"require": {
"php": "^7.2.5",
"ext-ctype": "*",
"ext-iconv": "*",
"api-platform/api-pack": "^1.2",
"doctrine/doctrine-migrations-bundle": "^2.1",
"lexik/jwt-authentication-bundle": "^2.6",
"locastic/api-platform-translation-bundle": "^1.3",
"symfony/console": "5.0.*",
"symfony/dotenv": "5.0.*",
"symfony/flex": "^1.3.1",
"symfony/framework-bundle": "5.0.*",
"symfony/translation": "5.0.*",
"symfony/yaml": "5.0.*",
"twig/extra-bundle": "^3.0"
},
"require-dev": {
"symfony/maker-bundle": "^1.14"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App": "src/"
}
},
"autoload-dev": {
"psr-4": {
"AppTests": "tests/"
}
},
"replace": {
"paragonie/random_compat": "2.*",
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php72": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "5.0.*"
}
}
}
I’ve tried to install “symfony/validator”: “^5.0.8”.
And the error message become :
[Semantical Error] The annotation “@AppFilterSearchAnnotation” in class AppEntityfoo was never imported. Did you maybe forget to add a “use” statement for this annotation?
Source: Symfony Questions
Was this helpful?
0 / 0