Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lifo101 opened this issue Feb 25, 2024 · 2 comments
Open

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

lifo101 opened this issue Feb 25, 2024 · 2 comments

Comments

@lifo101
Copy link

lifo101 commented Feb 25, 2024

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

@soyuka
Copy link
Member

soyuka commented Mar 15, 2024

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

@lifo101
Copy link
Author

lifo101 commented Mar 15, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants