Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!