Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
How can I introduce filtering functionality for a virtual property in Api-Platform?

I am using Symfony 5 and the API platform.

A class of mine has one of its properties set through a postLoad listener. The property is only set under certain conditions (otherwise it is NULL), and I would like to allow the REST API user to filter resources based on whether this property is null or has a value.

Because the virtual property is not persisted to the database, I am assuming that no Doctrine filters, e.g. the ExistsFilter, will work on this property.

How can I create filtering functionalities for virtual properties using Symfony 5 and the API-platform?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can create your own custom ORM filters.

An extremely simple example to show how would it be done:

Assuming a class:

Foo {

    public int $weight;

    public function isHeavy(): bool {
        return $this->weight > 40;
    }
}

Since heavy is a "virtual" property, you couldn't filter by it directly.

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

class HeavyFilter extends AbstractContextAwareFilter
{
    public function getDescription(string $resourceClass): array
    {
        // I'm making the filter available only 
        if (Foo::class !== $resourceClass) {
            return [];
        }

        
        if (!$this->properties) {
            return [];
        }

        $description                      = [];
        $description['heavySearch']   =
            [
                'property' => 'heavy',
                'type'     => 'bool',
                'required' => false,
                'swagger'  => [
                    'description' => 'Search for heavy foos',
                    'name'        => 'Heavey Search',
                    'type'        => 'bool',
                ],
            ];

        return $description;
    }

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

        if ($value === true) {
            $queryBuilder
                ->andWhere('o.weigth > 40');
        }
        else {
            $queryBuilder
                ->andWhere('o.weight <= 40');
        }
    }
}

Haven't actually tested this yet, just wrote it on the fly here, but the base idea is correct. You'd need to make adjustments to your own situation, and you'd have a custom filter that's even available on the Open Api docs.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda