5

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?

TheTom
  • 934
  • 2
  • 14
  • 40
  • I don't think this would be easy. Just curious: what kind of property are you setting to an entity, but not persisting? Why can't it be persisted? – Stephan Vierkant Jul 23 '21 at 11:34
  • And have you looked at [custom data providers](https://api-platform.com/docs/core/data-providers/)? – Stephan Vierkant Jul 23 '21 at 18:38
  • Thank you for your comments, yes I was considering to use a custom data provider, but am struggling to understand the pagination side of things. Would I need to create a custom paginator that paginates the (somehow filtered) set of resources and return this paginator in getCollection()? – NewCoder9191 Jul 24 '21 at 15:06

1 Answers1

1

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.

yivi
  • 42,438
  • 18
  • 116
  • 138
  • Hi yivi, many thanks for your comment. The virtual property in my application is not a mapped Doctrine field, so the queryBuilder returns a QueryException saying that the class has no such field or association. – NewCoder9191 Jul 28 '21 at 10:25
  • Hi @NewCoder9191 How did you manage to solve this problem? – ultimatum Jul 19 '22 at 10:32