core icon indicating copy to clipboard operation
core copied to clipboard

Unable to use HIDDEN fields in an ORM query within a custom Filter

Open lifo101 opened this issue 1 year ago • 3 comments

API Platform version(s) affected: 3.2.14

Description
I'm trying to create a custom filter that simply adds a special OrderBy to the QueryBuilder. This query requires the use of HIDDEN fields so Doctrine can sort by them. This query works in my non-api code. However, api-platform seems to think these values are joins and not fields and thus throws an exception of The alias "..." does not exist in the QueryBuilder..

How to reproduce

    protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
    {
        if ($property !== $this->searchTerm || !is_array($value)) {
            return;
        }

        if (!empty($value['template_order'])) {
            $a = $queryBuilder->getRootAliases()[0];
            $queryBuilder
                ->addSelect("CASE WHEN $a.enabled = 1 THEN 0 ELSE 1 END AS HIDDEN enabled")
                ->addSelect("CASE WHEN $a.quick = 1 THEN 0 ELSE 1 END AS HIDDEN quick")
                ->orderBy("enabled, quick, $a.title");
        }
    }

Possible Solution

Additional Context

lifo101 avatar Feb 25 '24 22:02 lifo101

What's the stack trace? Possible to have a reproducer? This should definitely work

soyuka avatar Mar 15 '24 10:03 soyuka

I can't do a reproducer at this time. But the stack trace is attached (JSON response): stack trace.txt

lifo101 avatar Mar 15 '24 16:03 lifo101

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 14 '24 23:05 stale[bot]